Python 3.3 Tutorial Notes - 7:Class

来源:互联网 发布:淘宝客服数据在哪看 编辑:程序博客网 时间:2024/06/03 03:51
1. Allow multiple base classes
2. namespace: Most namespaces are currently implemented as Python dictionaries
3. attribute: 
     3.1 for any name following a dot — for example, in the expression z.real
     3.2 Writable attributes may also be deleted with the del statement.
4. namespace life time
     4.1 The namespace containing the built-in names is created when the Python interpreter starts up, and is never deleted.
     4.2 The global namespace for a module is created when the module definition is read in
     4.3 normally, module namespaces also last until the interpreter quits.
     4.4 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.
     4.5 Recursive invocations each have their own local namespace.
5. Existing modules
     5.1 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__
     5.2 The built-in names actually also live in a module; this is called builtins
6. Scope:
     6.1 A scope is a textual region of a Python program where a namespace is directly accessible.
     6.2 “Directly accessible” here means that an unqualified reference to a name attempts to find the name in the namespace.
     6.3 scopes are determined statically, they are used dynamically.
          6.3.1 the innermost scope, which is searched first, contains the local names
          6.3.2 the scopes of any enclosing functions, which are searched starting with the nearest enclosing scope, contains non-local, but also non-global names
          6.3.3 the next-to-last scope contains the current module’s global names
          6.3.4 the outermost scope (searched last) is the namespace containing built-in names
     6.4 Sample
def scope_test():
  def do_local():
    spam = "local spam"
    print("do_local: " + spam)
  def do_nonlocal():
    nonlocal spam
    spam = "nonlocal spam"
    print("do_nonlocal: " + spam)
  def do_global():
    global spam
    spam = "global spam"
    print("do_global: " + spam)

  spam = "test spam"
  print("original: " + spam)
  print()
  do_local()
  print("After local assignment:", spam)
  print()
  do_nonlocal()
  print("After nonlocal assignment:", spam)
  print()
  do_global()
  print("After global assignment:", spam)
  print()

#spam = ''
scope_test()
print("In global scope:", spam)
----------------------------------------------------
original: test spam

do_local: local spam
After local assignment: test spam

do_nonlocal: nonlocal spam
After nonlocal assignment: nonlocal spam

do_global: global spam
After global assignment: nonlocal spam

In global scope: global spam
     6.5 if no global statement is in effect – assignments to names always go into the innermost scope.
     6.6 all operations that introduce new names use the local scope: in particular, import statements and function definitions bind the module or function name in the local scope.
7. Class could be defined in an if-branch or inside a function
8. When a class definition is entered, a new namespace is created, and used as the local scope
9. Class constructor: __init__(). If the class has this, it will be invoked when create new instance. It can has parameters, but first one is "self"
10. If an attribute is changed by <class_name>.<attribute_name>, it is changed for all instances which have not assign value to this attribute. If the instance object already set some value to this attribute, the change in class object will not impact it.
11. The instance object could contain some attributes which are not in the class object. Same thing can happen with "del".
12. The function in an instance object is called "method object", the function in a class object is called "function object"
13. A method object could be saved to another variable and use it later, before the instance object out of life scope
14. Python class is not designed to hide data. Taken care by programmer.
15. Need to manually maintain the name. Pay attention to override locally.
16. It is not necessary that the function definition is textually enclosed in the class definition: assigning a function object to a local variable in the class is also ok.
17. Each value is an object, and therefore has a class (also called its type). It is stored as object.__class__.
18. class DerivedClassName(modname.BaseClassName):
19. All methods in Python are virtual
20. a simple way to call the base class method directly: just call BaseClassName.methodname(self, arguments).
21. isinstance(<object>, <class_name>), issubclass(<class_name>, <base_class_name>)
22. For multiple inheritance, search for attribute is deep-first, left-to-right. For deeper understanding, there is a way to avoid a base class been searched more than once. 
23. There is NO private instance variable. A name with underscore as prefix should be non-public. This is an implementation detail and could be changed in the future.
24. name mangling: Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped.
25. Notice that code passed to exec() or eval() does not consider the classname of the invoking class to be the current class
26. Instance method objects have attributes, too: m.__self__ is the instance object with the method m(), and m.__func__ is the function object corresponding to the method.
27. if there are more than 1 "except" clause after "try", the first matched will be triggered and ignore other options.
28. iter(): create an iterator object
29. add iterator behavior to a class: define __iter__() in the class return an object which has __next__()
30. Generator: 
     30.1 Use "yield" statement to create an iterator: use "yield" other than "return" when you want to return something. then every time "__next__" is called, the value will return and resume the iteration in the method using "yield"
     30.2 the local variables and execution state are automatically saved between calls.
     30.3 when generators terminate, they automatically raise StopIteration.

Sample Code:
1. Iterator.py
2. Scope.py
3. Class1.py
4. Class2.py
0 0
原创粉丝点击