Python basics (10)-attributes

Python basics (10)-attributes

Attribute definition

Define directly in the class

class Student(object):
    gender ='male'

Constructor definition

class Student(object):
    gender ='male'
    def __init__(self,name,age):
        self.name = name
        self.age = age

stu = Student("BX",26)
print(stu.name,stu.age,stu.gender)
# BX 26 male

Private property

class Student(object):
    gender ='male'
    def __init__(self,name,age,score):
        self.name = name
        self.age = age
        self.__score = score
    def getScore(self):
        return self.__score


stu = Student("BX",26,90)
print(stu.__dict__)
print(stu.getScore())
print(stu._Student__score)
# {'name':'BX','age': 26,'_Student__score': 90}
# 90
# 90
  1. Private properties add getter and setter methods
class Money(object):
    def __init__(self):
        self.__money = 0

    def getMoney(self):
        return self.__money

    def setMoney(self, value):
        if isinstance(value, int):
            self.__money = value
        else:
            print("error:not an integer number")
  1. Use property to upgrade getter and setter methods
class Money(object):
    def __init__(self):
        self.__money = 0

    def getMoney(self):
        return self.__money

    def setMoney(self, value):
        if isinstance(value, int):
            self.__money = value
        else:
            print("error:not an integer number")
    money = property(getMoney, setMoney)
  1. Use property instead of getter and setter methods @property to become a property function, you can do necessary checks when assigning properties, and ensure that the code is clear and short. There are two main functions: converting the method to read-only and re-implementing the setting and reading of a property Method, can do boundary judgment
class Money(object):
    def __init__(self):
        self.__money = 0

    @property
    def money(self):
        return self.__money

    @money.setter
    def money(self, value):
        if isinstance(value, int):
            self.__money = value
        else:
            print("error:not an integer number")

Built-in attributes

When the subclass does not implement a __init__method, the parent class is automatically called by default. As defined __init__when the method, the need to manually call the parent class __init__method

Commonly used proprietary attributes

Description

Trigger method

__init__

Construct initialization function

After creating the instance, it is used when assigning, after __new__

__new__

Properties required to generate an instance

When creating an instance

__class__

The class the instance is in

Instance.__class__

__str__

Instance string representation, readability

print (class instance), if not implemented, use repr result

__repr__

Instance string representation, accuracy

Class instance carriage return or print (repr (class instance))

__del__

destruct

del delete instance

__dict__

Instance custom properties

vars(instance.__dict__)

__doc__

Class documentation, subclasses do not inherit

help (class or instance)

__getattribute__

Property access interceptor

When accessing instance properties

__bases__

All parent classes of the class constitute elements

Class name.__bases__

__dict__

__getattribute__

Get attributes

class Student(object):

    def __init__(self,name):
        self.name = name

    def __getattribute__(self, value):
        return super(Student,self).__getattribute__(value)
    def __setattr__(self, key, value):
        self.__dict__[key] = value

stu = Student("testStu")
print(stu.name)
Reference: https://cloud.tencent.com/developer/article/1437125 Python Basics (10)-Properties-Cloud + Community-Tencent Cloud