One: namespace
class Person: Country = 'Chinese' # static variable print(Person.Country) alex = Person() # creates an empty namespace alex.name = 'alex' # object alex.Country = 'Thai' egon = Person () stay.name = 'stay' # class name. static variable object. property name # Can the class name call the properties of the object? No # Can an object call properties in a class? Yes print(Person.Country) print(alex.Country) print(egon.Country)
print result
Note: The class name cannot call the property of the object (it is not clear which object property it is, two countries)
The object name can call the property in the class (alex.country)
Because there is an association between objects and classes So the object can find the class But the class can't find the object
2: Object. Attribute namespace search (first in your own namespace, then in the class namespace)
#Use the class name. The property will only look for the static variable name in the class #Using objects. Properties will first look for names in the object's own namespace # If you can't find it, go to the memory space of the class to find it class Person: Country = 'Chinese' # static variable alex = Person() egon = Person () print(alex.Country) alex.Country = 'Indian' print(alex.Country) Person.Country # As long as you use static variables, use the class name to call
Note: When you use static variables, use the class name to call
Example 1
class Person: money = 0 mother = Person() father = Person() Person.money += 1000 Person.money += 1000 print(Person.money) print(mother.money) print(father.money)
Print result: 2000, 2000, 2000
Example 2
class Person: money = [1] mother = Person() father = Person() mother.money[0] += 1000 [0] means index father.money[0] += 1000[0] means index print(mother.money) print(father.money) print(Person.money)
print result [2001], [2001], [2001]
Note: [0] means index, += 1000 means index plus.
Example 3
class Person: money = [0] mother = Person() father = Person() mother.money = [1000] father.money = [2000] print(mother.money) print(father.money) print(Person.money)
Print result: [1000], [2000], [0] In example 3, the index change only changes the calling class attribute, but this question does not change.
Example 4
a = 1 a = 2 print(a) a = [1] a.append(2) print(id(a)) a[0] = 10 print(id(a)) print(a) a = [1] a = [2]
Print result: 2, 40615688, 40615688, [10,2]
Example 5:
Write a class that can count how many objects this class has been instantiated. All objects share this result init static variable class Foo: num = 0 def __init__(self): Foo.num += 1 f1 = Foo() print(Foo.num) f2 = Foo() print(Foo.num) print(f1.num)
Combination: circle and teacher combination example
Combining two classes What is composition: properties of one class object are objects of another class A matter of two classes: there is a "what is what" relationship between classes round class rings and circles A torus is also a class Properties Great Circle Radius and Small Circle Radius Find the area of a ring to find the perimeter
from math import pi class Circle: def __init__(self,r): self.r=r def area(self): return pi*(self.r**2) class Ring: def __init__(self,outer,inner): self.outer=Circle(outer) self.inner=Circle(inner) def area(self): return self.outer.area()-self.inner.area() r=Ring(10,3) c=Circle(11) print(Ring.area(r)) print(r.area())
Example: Teacher's Birthday Combination
reference answer
teacher name sex course (course) birth birthday date class Birthday: def __init__(self,year,month,day): self.year = year self.month = month self.day = day class Teacher: def __init__(self,name,sex,course,birth): self.name = name self.sex = sex self.course = course self.birth = birth # birth is an object birth = Birthday(1960,3,7) # birth is an object of the Birthday class alex = Teacher('alex','male','python',birth) # alex.birth = birth # time '1960-3-7' # Teacher's age 2018 - 1960 print(birth) import time if birth.month == time.localtime().tm_mon and \ birth.day == time.localtime().tm_mday: print('Happy Birthday') print(time.localtime().tm_year - birth.year)
Write it yourself:
class Birthday: def __init__(self,year,month,day): self.year=year self.month=month self.day=day class Teacher: def __init__(self,name,course,birth): self.name=name self.course=course self.birth=birth birth=Birthday(1960,4,4) zhen=Teacher('zhen','python',birth) # print(Birthday.year()) print(zhen.birth.year) print(birth.year)