Skip to content Skip to sidebar Skip to footer

In Python, How Do I Reference A Class Generically In A Static Way, Like Php's "self" Keyword?

PHP classes can use the keyword 'self' in a static context, like this:

Solution 1:

This should do the trick:

classC(object):
    my_var = 'a'    @classmethoddeft(cls):
        print cls.my_var

C.t()

Solution 2:

In all cases, self.__class__ is an object's class.

http://docs.python.org/library/stdtypes.html#special-attributes

In the (very) rare case where you are trying to mess with static methods, you actually need classmethod for this.

classAllStatic( object ):
    @classmethoddefaMethod( cls, arg ):
        # cls is the owning class for this method 

x = AllStatic()
x.aMethod( 3.14 )

Post a Comment for "In Python, How Do I Reference A Class Generically In A Static Way, Like Php's "self" Keyword?"