Reference connection: http://www.cnblogs.com/wilber2013/p/4677412.html
class Student(object):
count = 0
books = []
def __int__(self,name,age):
self.name = name
self.age = age
pass #do nothing, an empty statement, ensures the integrity of format and semantics
Data properties
In the above Student class, "count", "books", "name" and "age" are called familiar data of class, but they are divided into class data attribute and instance data attribute
Class data properties and instance data properties
Student.books.extend(["python","javascript"])
print "Student book list: %s" %Student.books
Student.hobbies = ["reading", "jogging", "swimming"]
print "Student hobby list: %s" %Student.hobbies
wilber = Student("Wilber",28)
print "%s is %d years old" %(wilber.name, wilber.age)
# class instance can add new attribute
# "gender" is the instance attribute only belongs wilber
wilber.gender = "male"
print "%s is %s" %(wilber.name, wilber.gender)
# class instances can access class attribute
print dir(wilber)
wilber.books.append("C#")
#The built-in function dir() can be used to view the properties of a class
print wilber.books
will = Student("Will", 27)
print "%s is %d years old" %(will.name, will.age)
# will shares the same class attribute with wilber
# will don't have the "gender" attribute that belongs to wilber
print dir(will)
print will.books
###books Including wilber Added C#
For class data attributes and instance data attributes, they can be summarized as follows:
- Class data properties belong to the class itself and can be accessed / modified by the class name
- Class data properties can also be accessed / modified by all instances of the class
- After class definition, class data attributes can be added dynamically by class name, and the new attributes are shared by class and all instances
- Instance data properties can only be accessed through an instance
- After the instance is generated, the instance data attributes can be added dynamically, but they only belong to the instance
Property hiding
wilber = Student("Wilber", 28)
print "Student.count is wilber.count:", Student.count is wilber.count
wilber.count = 1
print "Student.count is wilber.count:", Student.count is wilber.count
print Student.__dict__
print wilber.__dict__
del wilber.count
print "wilber.count,Student.count:", wilber.count,Student.count
print wilber.__dict__
wilber.count += 3
print "wilber.count,Student.count:", wilber.count,Student.count
print Student.__dict__
print wilber.__dict__
wilber.books = ["C#","Python"]
print "wilber.books,Student.books:", wilber.books,Student.books
print Student.__dict__
print wilber.__dict__
del wilber.books
print Student.__dict__
print wilber.__dict__
print "wilber.books,Student.books:", wilber.books,Student.books
wilber.books.append("CSS")
print "wilber.books,Student.books:", wilber.books,Student.books
print Student.__dict__
print wilber.__dict__
Note: Although class properties can be accessed / modified by instance, it is not recommended to do so. It is better to access class properties by class name, so as to avoid unnecessary trouble caused by property hiding. For example, class instances modify class properties by append, but they cannot be modified in many cases