classes

Pablo pablo at bogus.domain.org
Sun Jul 20 08:30:11 EDT 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




More information about the Python-list mailing list