Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > classes

Reply
Thread Tools

classes

 
 
Pablo
Guest
Posts: n/a
 
      07-19-2003
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
 
Reply With Quote
 
 
 
 
Dan Bishop
Guest
Posts: n/a
 
      07-20-2003
"Pablo" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed). org>...
> 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)
 
Reply With Quote
 
 
 
 
Pablo
Guest
Posts: n/a
 
      07-20-2003
[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
 
Reply With Quote
 
Pablo
Guest
Posts: n/a
 
      07-20-2003
> 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
 
Reply With Quote
 
Michele Simionato
Guest
Posts: n/a
 
      07-21-2003
Steven Taschuk <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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
 
Reply With Quote
 
Steven Taschuk
Guest
Posts: n/a
 
      07-22-2003
Quoth Michele Simionato:
> Steven Taschuk <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...

[...]
> > _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."
http://www.velocityreviews.com/forums/(E-Mail Removed) Mark: "Turn-about is fair play, sir."
-- _Mirror Dance_, Lois McMaster Bujold

 
Reply With Quote
 
Michele Simionato
Guest
Posts: n/a
 
      07-23-2003
Steven Taschuk <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Quoth Michele Simionato:
> > Steven Taschuk <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...

> [...]
> > > _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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Classes within classes David ASP .Net 2 07-22-2005 07:13 PM
What is the difference between nested classes and inner classes ? Razvan Java 5 07-27-2004 07:59 PM
Modifiers applied to attributes, local variables, member functions, classes and inncer classes ! Razvan Java 11 07-17-2004 08:57 PM
Can I using reflection to get all child classes or classes undera package dynamically? Carfield Yim Java 1 05-31-2004 05:33 PM
How to access inner classes variables & methods from outer classes lonelyplanet999 Java 1 11-13-2003 01:54 PM



Advertisments