◦ Explicit is better than implicit. ◦ Simple is better than complex. ◦ Complex is better than complicated. ◦ Flat is better than nested. ◦ Sparse is better than dense. ◦ Readability counts. ◦ Special cases aren't special enough to break the rules. ◦ Although practicality beats purity. ◦ Errors should never pass silently. ◦ Unless explicitly silenced. ◦ In the face of ambiguity, refuse the temptation to guess. ◦ There should be one -and preferably only one- obvious way to do it. ◦ Although that way may not be obvious at first unless you're Dutch. ◦ Now is better than never. ◦ Although never is often better than right now. ◦ If the implementation is hard to explain, it's a bad idea. ◦ If the implementation is easy to explain, it may be a good idea. ◦ Namespaces are one honking great idea -let's do more of those!
= 256 b = 256 print (a is b) a = 257 b = 257 print (a is b) True False a = 256 b = 256 print id(a), id(b) a = 257 b = 257 print id(a), id(b) 22036112 22036112 22363568 22363640
= 256 b = 256 print (a is b) a = 257 b = 257 print (a is b) True False a = 256 b = 256 print id(a), id(b) a = 257 b = 257 print id(a), id(b) 22036112 22036112 22363568 22363640
current global symbol table. This is always the dictionary of the current module (inside a function or method, this is the module where it is defined, not the module from which it is called). locals() Update and return a dictionary representing the current local symbol table. Free variables are returned by locals() when it is called in function blocks, but not in class blocks.
This only have a meaningful value is the function is defined with “def”. __class__ This is a reference to the class a method belongs to. __code__ This is a reference to the code object used in the implementation of python.
squares = [x**2 for x in range(10)] [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y] { (k,v) for k,v in [(1,2)] } SET NOT DICT! { k:v for k,v in [(1,2)] }
"this is a class attribute" def __init__(self): self.attribute = "this is an obj attr override class one" self.another = "this is another obj attr, no class" print Example.attribute print Example().attribute print Example().another print Example.another
"this is a class attribute" def __init__(self): self.attribute = "this is an obj attr override class one" self.another = "this is another obj attr, no class" print Example.attribute print Example().attribute print Example().another print Example.another this is a class attribute this is an object attribute and override class one this is another object attribute, no class Traceback (most recent call last): Line 11, in <module> print Example.another AttributeError: type object 'Example' has no attribute 'another'
than a function defined inside a class. It is callable without instantiating the class first. It’s definition is immutable via inheritance. @classmethod Also callable without instantiating the class, but its definition follows Sub class, not Parent class, via inheritance. That’s because the first argument for @classmethod function must always be cls (class).
than a function defined inside a class. It is callable without instantiating the class first. It’s definition is immutable via inheritance. @classmethod Also callable without instantiating the class, but its definition follows Sub class, not Parent class, via inheritance. That’s because the first argument for @classmethod function must always be cls (class). @staticmethod def static_method(): print "I do not receive nothing :(" @classmethod def class_method(cls): print "I'm a class %s" % str(cls)
__init__(self, name): self.name = name @classmethod def class_method(cls, name): return cls(name) x = Example.class_method("example") print x <__main__.Example object at 0x40358b2c>
method to get called in an object's instantiation, is a @classmethod, and must return a new instance of type cls. __init__(self, *args, **kwargs) Is the initializer for the instance. It gets passed whatever the primary constructor was called with. __del__(self) Is the destructor of the instance, will be invoked before clean the reference to the instance.
((name, value) \ for name, value in f_class_attr.items() \ if not name.startswith('__')) uppercase_attr = dict((name.upper(), value) \ for name, value in attrs) return type(f_class_name, f_class_parents, uppercase_attr) class Foo(object): __metaclass__ = upper_attr bar = 'bip' print hasattr(Foo, 'bar') print hasattr(Foo, 'BAR')
((name, value) \ for name, value in f_class_attr.items() \ if not name.startswith('__')) uppercase_attr = dict((name.upper(), value) \ for name, value in attrs) return type(f_class_name, f_class_parents, uppercase_attr) class Foo(object): __metaclass__ = upper_attr bar = 'bip' print hasattr(Foo, 'bar') print hasattr(Foo, 'BAR') False True
n numbers from 0 to n-1. :type n: int :param n: the limit upper for the elements to be created (not included). :return: a class:`list` with the items. """ return range(0,n) class Example(object): """This is the documentation of the class. Usually you do not need it :) """ def __init__(self, param): """This is the documentation of the instance type. """