Article Directory
1, create a class property
Class is a template, and an object instance is created based on class.
Bound to an instance of the property does not affect other instances, however, the class itself is a target, if you bind a property on the class, all instances can access the class properties, and class attributes are all instances of access It is the same! In other words, each instance instance properties of their respective owners, independent of each other, but there is one and only one class attributes.
Defining class attributes may be defined directly in the class:
class Person(object):
address = 'Earth'
def __init__(self, name):
self.name = name
Because the class attribute is directly bound to the class, so do not need to create an instance of the class attribute access, you can directly access:
print Person.address
# => Earth
Calls for an instance of class property is also accessible to all instances have access to the property it belongs to the class:
p1 = Person('Bob')
p2 = Person('Alice')
print p1.address
# => Earth
print p2.address
# => Earth
Because Python is a dynamic language, class attributes also can be dynamically added or modified:
Person.address = 'China'
print p1.address
# => 'China'
print p2.address
# => 'China'
Because only one class attribute, so when the Person class of address change, access to all instances of the class attribute has changed.
task:
Please give the Person class to add a class attribute count, each creating an instance, count attribute is increased by one, so that you can create an instance of the statistics of the total number of Person.
CODE:
class Person(object):
count = 0
def __init__(self, name):
Person.count = Person.count + 1
self.name = name
p1 = Person('Bob')
print Person.count
# => 1
p2 = Person('Alice')
print Person.count
# => 2
p3 = Person('Tim')
print Person.count
# => 3
2, class and instance properties name conflict
how python class and instance properties name conflicts do
modify class attributes will cause all instances of the class have access to all properties are affected, however, if you modify the class attribute what will happen in the instance variables?
class Person(object):
address = 'Earth'
def __init__(self, name):
self.name = name
p1 = Person('Bob')
p2 = Person('Alice')
print 'Person.address = ' + Person.address
p1.address = 'China'
print 'p1.address = ' + p1.address
print 'Person.address = ' + Person.address
print 'p2.address = ' + p2.address
The results are as follows:
Person.address = Earth
p1.address = China
Person.address = Earth
p2.address = Earth
We found that the set p1.address = 'China'
after, p1 access address does become 'China'
, however, Person.address
and p2.address
continues to be 'Earch'
, how is it?
The reason is that p1.address = 'China'
did not change address Person, but this instance is bound to p1 instance property address, for p1, it has an address instance attribute (value 'China'), while it belongs to class Person class also has a property address, so:
When accessing p1.address, priority to find examples of property that returns the 'China'.
When accessing p2.address, p2 no instance attribute address, but there is a class attribute address, the flow returns 'Earth'.
Be seen, when the instance attributes and class attributes same name, instance attribute high priority, it will shield the access class attributes.
When we attribute to delete the address instance p1, visit p1.address it returned valued properties of 'Earth' of:
del p1.address
print p1.address
# => Earth
Visible, do not change the class attribute on an instance, it does not actually change the class attribute, but the instance is bound to an instance attribute.
task:
Please put on the section of the Person class attribute count changed __count, and then see if I can access the property from the instance and class.
CODE:
class Person(object):
__count = 0
def __init__(self, name):
Person.__count = Person.__count + 1
self.name = name
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except AttributeError:
print 'attributeerror'