Thursday, 13 October 2011

Python class members

Static and Class methods

class A(object):
   height = 0

   @classmethod
   def jump(cls,a):
       cls.height+=a

   @staticmethod
   def sum(a,b):
       return a+b

Protected and Private members

class A(object):
    SINGLETONS={}
class B(A):
    pass

hasattr(B,'SINGLETONS')
Out:True

class A(object):
    _SINGLETONS={}
class B(A): 
    pass

hasattr(B,'_SINGLETONS')
Out:True

class A(object):
    __SINGLETONS={}
class B(A): 
    pass

hasattr(B,'__SINGLETONS')
Out:False

No comments:

Post a Comment