Python类概况:多继承、override、多态
A namespace is a mapping from names to objects. Most namespaces are currently implemented as Python dictionaries, but that’s normally not noticeable in anyway (except for performance), and it may change in the future. Examples of namespaces are: the set of built-in names (containing functions such asabs(), and built-in exception names);the global names in a module; and the local names ina function invocation.
The important thing to know about namespaces is that there is absolutely no relation between names in different namespaces; for instance, two different modules may both define a functionmaximize without confusion —users of the modules must prefix it with the module name.
生存周期
Namespaces are created at different moments and have different lifetimes. The namespace containing the built-in names is created when the Python interpreter starts up, and is never deleted. The global namespace for a module is created when the module definition is read in; normally, module namespaces also last until the interpreter quits. The statements executed by the top-level invocation of the interpreter, either read from a script file or interactively,are considered part of a module called__main__, so they have their ow nglobal namespace. (The built-in names actually also live in a module; this iscalled__builtin__.)
The local namespace for a function is created when the function is called, and deleted when the function returns or raises an exception that is not handled within the function.class ClassName: <statement-1> . . . <statement-N>
类对象支持两种操作:引用和实例化
Class objects support two kinds of operations: attribute references andinstantiation.
Attribute references use the standard syntax used for all attribute referencesin Python:obj.name.
class MyClass: """A simple example class""" i = 12345 def f(self): return 'hello world'then MyClass.i and MyClass.f are valid attribute references, returning an integer and a function object, respectively.
x = MyClass()
Data attributes need not be declared
x.counter = 1 while x.counter < 10: x.counter = x.counter * 2 print x.counter del x.counterA method is a function that “belongs to” an object.
xf = x.f while True: print xf()函数调用时类实例作为第一个参数隐式传入
Methods in the same class may call other methods by using method attributes of theself argument:
class Bag: def __init__(self): self.data = [] def add(self, x): self.data.append(x) def addtwice(self, x): self.add(x) self.add(x)
class DerivedClassName(BaseClassName): <statement-1> . . . <statement-N>Derived classes may override methods of their base classes.(For C++ programmers: all methods in Python are effectively virtual.)
class DerivedClassName(Base1, Base2, Base3): <statement-1> . . . <statement-N>
没太看懂o(╯□╰)o
class Employee: pass john = Employee() # Create an empty employee record # Fill the fields of the record john.name = 'John Doe' john.dept = 'computer lab' john.salary = 1000
class B: pass class C(B): pass class D(C): pass for c in [B, C, D]: try: raise c() except D: print "D" except C: print "C" except B: print "B"
Note that if the except clauses were reversed (with except B first), it would have printed B, B, B — the first matching except clause is triggered.
By now you have probably noticed that most container objects can be looped over using a for statement:
for element in [1, 2, 3]: print element for element in (1, 2, 3): print element for key in {'one':1, 'two':2}: print key for char in "123": print char for line in open("myfile.txt"): print line,另一种方式 the for statement calls iter()
>>> s = 'abc' >>> it = iter(s) >>> it <iterator object at 0x00A1DB50> >>> it.next() 'a' >>> it.next() 'b' >>> it.next() 'c' >>> it.next() Traceback (most recent call last): File "<stdin>", line 1, in ? it.next() StopIteration
def reverse(data): for index in range(len(data)-1, -1, -1): yield data[index] >>> for char in reverse('golf'): ... print char ... f l o g