staticmethod() problems

Aum spam-me at no-thanks.com
Thu Jan 23 02:26:34 CET 2003


Hi,

I'm trying to find a way for a static method in a class to determine if it 
was called from the class, or from an instance.

For example:

>>>class C:
...   def mystaticfunc():
...      if i_was_called_statically:
...         print "I've been called statically"
...      else:
...         inst = some_magic_way_of_finding_the_instance_invoking_me
...         print "I was invoked via instance ", inst
...   mystaticfunc = staticmethod(mystaticfunc)
...
>>>c = C()
>>>C.mystaticfunc()
I've been called statically
>>>c.mystaticfunc()
I was invoked via instance <modulename.C instance at 0x6239384>
>>>class C1(C):
...   pass
...
>>>c1 = C1()
>>>c1.mystaticfunc()
I was invoked via instance <modulename.C1 instance at 0x3426573>
>>>

Is there any way to do this without having to define 2 separate methods?

Thanks for your help
A





More information about the Python-list mailing list