Object-Oriented Class Membership of Python Full Stack Road Series

The existence of a feature is to disguise a method as a field.

property

Class methods are called as common fields, that is, they are called with objects without parentheses.

#!/usr/bin/env python
# _*_coding:utf-8 _*_

class Foo:

    @property
    def Characteristic(self):
        print("Characteristics of Class Method")

# Create an object
obj = Foo()
# When calling a class method, there is no parentheses after the method
obj.Characteristic

output

/usr/bin/python3.5 /home/ansheng/File/Python_code/sublime/Week06/Day04/s2.py
//Characteristics of Class Method

Process finished with exit code 0

setter

Set the value of the class method

What can I learn from my learning process?
python Learning Exchange Button qun,784758214
//There are good learning video tutorials, development tools and e-books in the group.
//Share with you python enterprise talent demand and how to learn python from zero basis, and learn what content.
#!/usr/bin/env python
# _*_coding:utf-8 _*_

class Foo:

    # Get Characteristic value
    @property
    def Characteristic(self):
        return "Obtain Characteristic value"

    # The following Characteristic function is used to set values for the Characteristic function above.
    @Characteristic.setter
    def Characteristic(self, value):
        return "Set up Characteristic Value"

# Create an object
obj = Foo()

# Get the value of Characteristic
print(obj.Characteristic)

# Set Characteristic's value
obj.Characteristic = 123

output

/usr/bin/python3.5 /home/ansheng/File/Python_code/sublime/Week06/Day04/s2.py
//Get Characteristic value

Process finished with exit code 0

deleter

class Foo:

    # Special field
    @property
    def pp(self):
        # Output aaa when calling special fields
        print("property")

    @pp.setter
    def pp(self, value):
        # Output value when calling setup method
        print(value)

    @pp.deleter
    def pp(self):
        # Output del when calling deletion method
        print("deleter")

# Create an object obj
obj = Foo()

# Automatically execute @property
obj.pp

# Automatically execute @pp.setter
obj.pp = 999

# Automatically execute @pp.deleter
del obj.pp

output

ansheng@Darker:~$ python3 /home/ansheng/File/Python_code/sublime/Week07/s1.py
property
999
deleter

Another way to call special attributes

class Foo:

    def f1(self):
        print("f1")

    def f2(self, value):
        print("f2")

    def f3(self):
        print("f3")

    SpecialFields = property(fget=f1, fset=f2, fdel=f3, doc="I am an annotation.")

# Create an object
obj = Foo()

# f1 method of calling class
obj.SpecialFields

# f2 method of calling class
obj.SpecialFields = 123

# Invoke the method of sending a class
del obj.SpecialFields

# Call the doc of the class, which can only be accessed through the class, and the object can not be accessed.
print(Foo.SpecialFields.__doc__)

Output result

ansheng@Darker:~$ python3 /home/ansheng/File/Python_code/sublime/Week07/s1.py
f1
f2
f3
//I am an annotation.

If you are still confused in the world of programming, you can join our Python learning button qun: 784758214 to see how our predecessors learned! Exchange experience! I am a senior Python development engineer, from basic Python scripts to web development, crawler, django, data mining, etc. To every little friend of Python! Share some learning methods and small details that need attention. Click to join us. python learner gathering place

Tags: Python sublime Programming Web Development

Posted on Fri, 04 Oct 2019 05:06:18 -0700 by greens85