The Meaning of Class Definitions
2013-05-31 22:17
288 查看
we have to keep in mind that the meaning of class definitions are simply
code sections that are executed !
In the preceding code, a variable is defined in the class scope, which can be accessed by all the members (instances), in this case to count the number of class members.
This class scope variable is accessible from every instance as well, just as methods are:
What happens when you rebind the members attribute in an instance?
The new members value has been written into an attribute in m1, shadowing the class-wide variable. This mirrors the behavior of local and global variables in functions.
from above, we see, once you rebind the members attribute in an instance, a instance-local variable was create. Otherwise the m2.members refer to (or point to) the MemberCounter.members,
that is, instance.memberAttribute are bound to the class.memberAttribute.
code sections that are executed !
>>>class C: print 'Class C being defined...' Class C being defined... >>>class MemberCounter: members = 0 def init(self): MemberCounter.members += 1 >>>m1 = MemberCounter() >>>m1.init() >>>MemberCounter.members 1 >>>m2 = MemberCounter() >>>m2.init() >>>MemberCounter.members 2
In the preceding code, a variable is defined in the class scope, which can be accessed by all the members (instances), in this case to count the number of class members.
This class scope variable is accessible from every instance as well, just as methods are:
>>>m1.members 2 >>>m2.members 2
What happens when you rebind the members attribute in an instance?
>>> m1.members = 'Two' >>> m1.members 'Two' >>> m2.members 2
The new members value has been written into an attribute in m1, shadowing the class-wide variable. This mirrors the behavior of local and global variables in functions.
>>>MemberCounter.members = 10 10 >>>m1.members 'Two' >>>m2.members 10
from above, we see, once you rebind the members attribute in an instance, a instance-local variable was create. Otherwise the m2.members refer to (or point to) the MemberCounter.members,
that is, instance.memberAttribute are bound to the class.memberAttribute.
相关文章推荐
- C++ philosophy: Changing the accessibility of a class member should never change the meaning of a program.
- the usage of final class instance
- The Meaning of URL, URI, URN
- StructLayoutAttribute- control the physical layout of the data fields of a class or structure.
- Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP
- How to fix the dreaded "java.lang.OutOfMemoryError: PermGen space" exception (classloader leaks)
- Inheritance is the base class of evil (Sean Parent)
- Question 26: Which of the following options describe the expected overhead for a class that has 5 virtual functions?
- Question 30: What is the order of destructor calls for an object of class Y inherited from class X that has an object of class A
- 凯撒密码 CH Round #57 - Story of the OI Class
- The source code of the Account class of the famous IM app
- How can I protect derived classes from breaking when I change the internal parts of the base class?
- The serializable class XXX does not declare a static final serialVersionUID field of type long
- The content of element type "class" must match "(meta*,subselect?,cache?,synchronize*,comment?,tupli
- 关于问题The fully qualified name of the bean's class, except if it serves only as a parent definition fo
- Wen Shenzhen line, then possession of the profound meaning of political reform
- The 3rd class of Josh
- the main methos of class Ssh in pyssh module
- The Meaning of Open (from google's offical blog)
- The JAR of this class file belongs to container 'Android Private Libraries' Android查看外部依赖jar的源码问题解