classes

Discussion in 'Python' started by Pablo, Jul 19, 2003.

  1. Pablo

    Pablo Guest

    Hello everyone

    I have a problem with classes and hope someone can help me.
    I'm used to Java and want to make a singleton class which would allow to
    create only one instance of the class.
    In Java it looks as follows:
    public class DBConnection{
    private static DBConnection instance = null;
    private Conn conn = null; // it is supposed to be object returned be
    pg.connect() method of the PyGreSQL module

    /* a constructor must be private to forbid instantiation of the class */
    private DBConnection(){
    conn = new Conn();
    }

    public static DBConnection getInstance(){
    if(instance == null)
    instance = new DBConnection();
    return instance;
    }
    };

    How can I do the same in Python?
    My main problems are:
    1) how to declare a static field (class field not object field)
    2) how to declare a static method
    3) how to declare a private constructor (is it possible?)

    Can someone help me or point me to some documentation with strong emphasis
    on classes? It would be perfect if that documentation had some comparisons
    with classes in Java or C++.

    Best regards
    Pablo
    Pablo, Jul 19, 2003
    #1
    1. Advertising

  2. Pablo

    Dan Bishop Guest

    "Pablo" <> wrote in message news:<>...
    > Hello everyone
    >
    > I have a problem with classes and hope someone can help me.
    > I'm used to Java and want to make a singleton class which would allow to
    > create only one instance of the class.

    ....[snip]...
    > How can I do the same in Python?
    > My main problems are:
    > 1) how to declare a static field (class field not object field)


    <nitpick>You can't. There are no variable declarations in
    Python.</nitpick> You can use static fields, though.

    >>> class HasStaticField:

    .... field = 4
    ....
    >>> HasStaticField.field

    4

    > 2) how to declare a static method


    class HasStaticMethod:
    def method(arg0, arg1): # note the lack of "self"
    # do something with arg0 and arg1
    method = staticmethod(method)

    > 3) how to declare a private constructor (is it possible?)


    It's not possible (afaik) to define a private constructor. But it is
    possible to enforce that a constructor can be called only once.

    class SingletonError(Exception):
    pass

    class Singleton:
    __instance = None
    def __init__(self):
    if Singleton.__instance is None:
    # initialization code
    Singleton.__instance = self
    else:
    raise SingletonError()
    def getInstance():
    if Singleton.__instance is None:
    Singleton()
    return Singleton.__instance
    getInstance = staticmethod(getInstance)
    Dan Bishop, Jul 20, 2003
    #2
    1. Advertising

  3. Pablo

    Pablo Guest

    [cut]
    >> 2) how to declare a static method

    >
    > class HasStaticMethod:
    > def method(arg0, arg1): # note the lack of "self"
    > # do something with arg0 and arg1
    > method = staticmethod(method)


    That's not exactly what I wanted.
    I would prefer something like this:
    class StaticMethod:
    __instance = None
    def __init__(self):
    if StaticMethod.__instance is None:
    StaticMethod.__instance = self
    else:
    raise Exception("Constructor may be invoked only once")
    def getInstance():
    if StaticMethod.__instance is None:
    StaticMethod.__instance = StaticMethod()
    return StaticMethod.__instance

    m = StaticMethod.getInstance()
    but Python does not allow to invoke any class method without providing an
    instance of the class object

    I've been thinking about Python classes and modules and found out that it
    is possible in Python to create an object in some module and use a
    reference to it from other modules. In Java it's not possible to create
    any object outside of any class.
    So my problem would be solved if I could create a class which would be
    seen only in its module (something like private class). Then I could
    create an object and simply use it across my application.
    It would behave like a static object since it
    wouldn't be possible to create another object in other modules (since the
    class would be visible only it its module), and it also wouldn't be
    possible to create another object in its module (there is no reason for
    it).

    Is it possible to create a class and forbid its use outside its module?


    Thanks for a usefull reply.
    Pablo
    Pablo, Jul 20, 2003
    #3
  4. Pablo

    Pablo Guest

    > Sure it does. Allowing that is the purpose of the "staticmethod"
    > call in Dan's example.


    Yes You're right and Dan as well.
    I just didn't know that that 'staticmethod' existed and tried to do "Java
    style" static method which obviously didn't work

    The trick with __new__ is usefull. I must read some more about classes in
    Python. Thanks to Pedro for a link.

    Cheers
    Pablo
    Pablo, Jul 20, 2003
    #4
  5. Steven Taschuk <> wrote in message news:<>...
    > For the particular problem you're interested in -- singletons --
    > here are a few approaches:
    >
    > First, use __new__ trickery:
    >
    > _the_instance = None
    > class MySingleton(object):
    > def __new__(self):
    > global _the_instance
    > if _the_instance is None:
    > _the_instance = object.__new__(self)
    > return _the_instance


    Why are you using a global here and not something like


    class MySingleton(object):
    _the_instance = None
    def __new__(cls):
    if cls._the_instance is None:
    cls._the_instance = object.__new__(self)
    return cls._the_instance

    ?

    > Example use:
    >
    > >>> x = MySingleton()
    > >>> y = MySingleton()
    > >>> x is y # same object!

    > True
    >
    > In this approach, users create instances of MySingleton as they
    > would for any other class (rather than by calling a getInstance
    > classmethod) -- that action just happens to return the same object
    > always.
    >
    > One gotcha with this approach can be observed by adding
    >
    > def __init__(self):
    > print 'running __init__ on instance %s' % id(self)
    >
    > Then we see
    >
    > >>> x = MySingleton()

    > running __init__ on instance 1075795852
    > >>> y = MySingleton()

    > running __init__ on instance 1075795852
    > >>> x is y

    > True
    >
    > As shown, each "instantiation" runs __init__ on the single
    > instance again. If you have initialization which should occur
    > only when the single instance is actually created:
    >
    > _the_instance = None
    > class MySingleton(object):
    > def __new__(self):
    > global _the_instance
    > if _the_instance is None:
    > _the_instance = object.__new__(self)
    > _the_instance._my_init()
    > return _the_instance
    > def _my_init(self):
    > pass # one-time initialization here
    >
    > (Another possible issue with this approach arises when subclassing
    > MySingleton. Details left as an exercise.)
    >
    > Second approach: Use a metaclass. See
    > <http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/102187>



    Unfortunately, I see that this recipe is not very recommendable. I have
    just submitted a fix which seems to work:

    class Singleton(type):
    def __init__(cls,name,bases,dic):
    super(Singleton,cls).__init__(name,bases,dic)
    cls.instance=None
    def __call__(cls,*args,**kw):
    if cls.instance is None:
    cls.instance=super(Singleton,cls).__call__(*args,**kw)
    return cls.instance

    Here is how it works under inheritance and avoids the problem with
    calling __init__ twice:

    class C:
    __metaclass__=Singleton
    def __init__(self):
    print "C: initializing ",self

    class D(C):
    def __init__(self):
    print "D: initializing ",self

    c1=C() # => C: initializing <__main__.C object at 0x4031c0ac>
    c2=C() # no output
    d=D() # D: initializing <__main__.D object at 0x4031c02c>
    print c1 is c2 # => yes


    > Third: forget about singletons and use a Borg. See
    > <http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531>
    > (With Borgs, multiple instances may exist, but they share state.)
    >
    > Fourth: rethink the idea that a database connection should be a
    > singleton. See
    > <http://c2.com/cgi/wiki?SingletonsAreEvil>
    > and linked pages for discussion on the merits of singletons.



    HTH,
    Michele
    Michele Simionato, Jul 21, 2003
    #5
  6. Quoth Michele Simionato:
    > Steven Taschuk <> wrote in message news:<>...

    [...]
    > > _the_instance = None
    > > class MySingleton(object):
    > > def __new__(self):
    > > global _the_instance
    > > if _the_instance is None:
    > > _the_instance = object.__new__(self)
    > > return _the_instance

    >
    > Why are you using a global here and not [a class attribute]


    The memory of that thread a little while back about using __del__
    with singletons. If the instance is referenced by a class
    attribute, the cyclic reference prevents the __del__ from being
    used. If the cycle goes through a module attribute, though, the
    zapping of module dicts during shutdown breaks the cycle and lets
    the __del__ run. (Whether all this is true depends on the version
    of Python, I think, but I don't know the details.)

    This might be relevant to the OP, whose example was a singleton
    representing the single database connection used by an entire
    application -- in such a case, __del__ would be a natural place to
    make sure the connection is closed properly.

    I should have explained this bit of trickery. :(

    [...]
    > > Second approach: Use a metaclass. See
    > > <http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/102187>

    >
    >
    > Unfortunately, I see that this recipe is not very recommendable. I have
    > just submitted a fix which seems to work:

    [...]

    Nice!

    --
    Steven Taschuk Aral: "Confusion to the enemy, boy."
    Mark: "Turn-about is fair play, sir."
    -- _Mirror Dance_, Lois McMaster Bujold
    Steven Taschuk, Jul 22, 2003
    #6
  7. Steven Taschuk <> wrote in message news:<>...
    > Quoth Michele Simionato:
    > > Steven Taschuk <> wrote in message news:<>...

    > [...]
    > > > _the_instance = None
    > > > class MySingleton(object):
    > > > def __new__(self):
    > > > global _the_instance
    > > > if _the_instance is None:
    > > > _the_instance = object.__new__(self)
    > > > return _the_instance

    > >
    > > Why are you using a global here and not [a class attribute]

    >
    > The memory of that thread a little while back about using __del__
    > with singletons. If the instance is referenced by a class
    > attribute, the cyclic reference prevents the __del__ from being
    > used. If the cycle goes through a module attribute, though, the
    > zapping of module dicts during shutdown breaks the cycle and lets
    > the __del__ run. (Whether all this is true depends on the version
    > of Python, I think, but I don't know the details.)
    >
    > This might be relevant to the OP, whose example was a singleton
    > representing the single database connection used by an entire
    > application -- in such a case, __del__ would be a natural place to
    > make sure the connection is closed properly.
    >
    > I should have explained this bit of trickery. :(
    >

    Thanks for the explation, I missed that thread.

    Michele
    Michele Simionato, Jul 23, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David

    Classes within classes

    David, Jul 21, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    4,927
    David
    Jul 22, 2005
  2. lonelyplanet999
    Replies:
    1
    Views:
    2,201
    VisionSet
    Nov 13, 2003
  3. Carfield Yim
    Replies:
    1
    Views:
    1,399
    Andrew Thompson
    May 31, 2004
  4. Razvan
    Replies:
    11
    Views:
    898
    Andrew Thompson
    Jul 17, 2004
  5. Razvan
    Replies:
    5
    Views:
    11,282
    Dale King
    Jul 27, 2004
Loading...

Share This Page