¡@

Home 

python Programming Glossary: cls

What is a metaclass in Python?

http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python

look like this class UpperAttrMetaclass type def __new__ cls clsname bases dct uppercase_attr for name val in dct.items .. like this class UpperAttrMetaclass type def __new__ cls clsname bases dct uppercase_attr for name val in dct.items if not.. val else uppercase_attr name val return type.__new__ cls clsname bases uppercase_attr We can make it even cleaner by..

What is the difference between @staticmethod and @classmethod in Python?

http://stackoverflow.com/questions/136097/what-is-the-difference-between-staticmethod-and-classmethod-in-python

print executing foo s s self x @classmethod def class_foo cls x print executing class_foo s s cls x @staticmethod def static_foo.. def class_foo cls x print executing class_foo s s cls x @staticmethod def static_foo x print executing static_foo.. With staticmethods neither self the object instance nor cls the class is implicitly passed as the first argument. a.static_foo..

Python's use of __new__ and __init__?

http://stackoverflow.com/questions/674304/pythons-use-of-new-and-init

. Here's an example class A object _dict dict def __new__ cls if 'key' in A._dict print EXISTS return A._dict 'key' else print.. EXISTS return A._dict 'key' else print NEW return super A cls .__new__ cls def __init__ self print INIT A._dict 'key' self.. A._dict 'key' else print NEW return super A cls .__new__ cls def __init__ self print INIT A._dict 'key' self print a1 A a2..

Creating a singleton in python

http://stackoverflow.com/questions/6760685/creating-a-singleton-in-python

3 A metaclass class Singleton type _instances def __call__ cls args kwargs if cls not in cls._instances cls._instances cls.. Singleton type _instances def __call__ cls args kwargs if cls not in cls._instances cls._instances cls super Singleton cls.. type _instances def __call__ cls args kwargs if cls not in cls._instances cls._instances cls super Singleton cls .__call__..