Unique features custom objects

Class defined function is a function attribute class, a class can use, but use is a normal function of it, implies the need for full compliance with the rules of function parameters, which pass several values ​​to pass several

Introduced

class OldboyStudent:
    school = 'oldboy'

    def choose_course(self):
        print('is choosing course')


stu1 = OldboyStudent()
stu2 = OldboyStudent()
stu3 = OldboyStudent()
  • For the above-mentioned class of students if the class attribute changed, the properties of other objects will change
OldboyStudent.school = 'OLDBOY'
print(stu1.school)
OLDBOY
print(stu2.school)
OLDBOY

Unique features custom objects

84- customized objects unique characteristics - like currency .jpg

print(stu1.__dict__)
{}
print(stu2.__dict__)
{}
  • Nature object similar to a class, but also a name space, but the name of the object space to store the object unique name, and class objects are stored in a common name. Therefore, we can directly target custom name alone.
stu1.name = 'tank'
stu1.age = 18
stu1.gender = 'male'

print(stu1.name, stu1.age, stu1.gender)
haoge 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)
zhuge 19 female

Find properties

  • First look for from their own, did not find to find a class, the class will be no error. Find a property that is the object of the order: self - "Class -" error

Custom attribute class definition phase

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)
haoge1 181 male1
init(stu2, 'sean1', 191, 'female1')
print(stu2.name, stu2.age, stu2.gender)
zhuge1 191 female1
  • Using the above method, although let's custom properties is simpler, but still too much trouble, if the timing properties can be triggered automatically when an object is instantiated, it is more convenient, you can use __init__ method of the class.

84- customized objects unique characteristics - complex .jpg

class OldboyStudent:
    school = 'oldboy'

    def __init__(self, name, age, gender):
        """调用类的时候自动触发"""
        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')
**************************************************
  • Can be found through the above phenomenon occurs when the calling class two things:
    1. Create an empty object
    2. Automatic trigger class __init__ function will stu1 and parameters within the parentheses with the incoming call class
print(stu1.__dict__)
{'name': 'nick', 'age': 18, 'gender': 'male'}

Guess you like

Origin www.cnblogs.com/nickchen121/p/10986649.html