Links to the original text: https://www.jianshu.com/u/8f2987e2f9fb
The function defined in the class is the function attribute of the class, which can be used by the class, but only a common function is used, which means that the parameter rules of the function should be obeyed completely, and the values of the function should be passed several times.
I. Introduction
class OldboyStudent: school = 'oldboy' def choose_course(self): print('is choosing course') stu1 = OldboyStudent() stu2 = OldboyStudent() stu3 = OldboyStudent()
- For the above-mentioned student classes, if the attributes of the classes are changed, the attributes of other objects will also be changed.
OldboyStudent.school = 'OLDBOY' print(stu1.school) OLDBOY print(stu2.school) OLDBOY
2. Unique Characteristics of Customized Objects
print(stu1.__dict__) {} print(stu2.__dict__) {}
- Objects are similar in nature to classes and are also namespaces, but the object's namespace stores the object's unique name, while the class stores the object's common name. So we can customize the name for the object directly.
''' //Nobody answered the question? Editor created a Python learning and communication QQ group: 857662006 //Look for like-minded friends, help each other, there are good video learning tutorials and PDF e-books in the group! ''' stu1.name = 'tank' stu1.age = 18 stu1.gender = 'male' print(stu1.name, stu1.age, stu1.gender) tank 18 male try: print(stu2.name, stu2.age, stu2.gender) except Exception as e: print(e) 'OldboyStudent' object has no attribute 'name' stu2.name = 'sean' stu2.age = 19 stu2.gender = 'female' print(stu2.name, stu2.age, stu2.gender) sean 19 female
3. Attribute Search
- First of all, from their own search, did not find to find in the class, the class will not report errors if there is No. That is to say, the order of the object's attribute search is: self-class-error reporting.
IV. Customization Attributes in Class Definition Stage
''' //Nobody answered the question? Editor created a Python learning and communication QQ group: 857662006 //Look for like-minded friends, help each other, there are good video learning tutorials and PDF e-books in the group! ''' def init(obj, x, y, z): obj.name = x obj.age = y obj.gender = z init(stu1, 'tank1', 181, 'male1') print(stu1.name, stu1.age, stu1.gender) tank1 181 male1 init(stu2, 'sean1', 191, 'female1') print(stu2.name, stu2.age, stu2.gender) sean1 191 female1
- Although using the above method makes it easier to customize attributes, it's still too cumbersome. It's more convenient to automatically trigger timing attributes when instantiating objects, so class__init_ method can be used.
''' //Nobody answered the question? Editor created a Python learning and communication QQ group: 857662006 //Look for like-minded friends, help each other, there are good video learning tutorials and PDF e-books in the group! ''' class OldboyStudent: school = 'oldboy' def __init__(self, name, age, gender): """Triggered automatically when calling a class""" self.name = name self.age = age self.gender = gender print('*' * 50) def choose_course(self): print('is choosing course') try: stu1 = OldboyStudent() except Exception as e: print(e) __init__() missing 3 required positional arguments: 'name', 'age', and 'gender' stu1 = OldboyStudent('nick', 18, 'male') **************************************************
- From the above phenomena, two things happen when calling a class:
- Create an empty object
- The execution of the _init_ function in the automatic trigger class passes stu1 along with the parameters in the call class brackets
print(stu1.__dict__) {'name': 'nick', 'age': 18, 'gender': 'male'}