The examples in this article describe the techniques used in the use of properties and methods in Python programming. Share to everyone for your reference. The specific analysis is as follows:
In Python, attributes are divided into public and private properties, and public properties can be invoked outside the class, and private properties cannot be invoked outside of the class. A public property can be any variable, and a private property is a variable that starts with a double underscore.
Here we define a people class that has a public property name and a private property __age.
|1 2 3 4||Class people (): Def __init (self): self.name= ' Zhang Shan ' self.__age=24|
We create an instance of the People class, P1, when we call its private property __age, we find the following error.
Traceback (most recent call last):
Attributeerror: ' People ' object has no attribute ' __age '
This means that private properties cannot be used outside of a class. So if we want to call the value of a private property, we can call it inside the class by defining a method.
|1 2 3 4 5 6 7 8 9||>>> class People (): Def __init__ (self): self.name= ' Jack ' self.__age=23 def Showinfo (self): print (Self.__age) & Gt;>> p2=people () >>> p2.showinfo () 23|
One might ask, why is it that we can't invoke the outside of a class by adding a double underscore attribute? So let's explore the Python object-oriented private mechanism.
In Python, properties and methods that begin with a double underscore automatically add _classname to their name before they are instantiated. Because the name has been changed, nature cannot be accessed by the name that begins with the double down line, thus achieving an unreachable purpose.
We can view the collection of properties for an object by using the instance name. __dict__.
Unlike other object-oriented programming languages in Python, Python's philosophy of design is simply paramount, so if you really want to invoke private properties, you can call them.
Second, the method
In Python, methods are divided into public, private, class, and static methods.
Now let's look at a more complete example
|1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32||#!/usr/bin/python #coding: Utf-8 __author__ = ' Mxi4oyu ' class people (): Def __init__ (self): self.name= ' Zhang Shan ' self.__age=23 D EF fun1 (self): #共有方法可以在类的外部进行调用 #可以通过对象名. Method name to call print ("Common method") def __fun2 (self): #私有方法不能在类的外部进行调用 #可以在类的其他方法中调用私有方法 Print (Private method) def funcshow (self): self.__fun2 () @classmethod #类方法要加上 @classmethod modifier, which can invoke Def fun3 (self) by using the class name. Method Name: Print ("class method") @staticmethod #静态方法需要加上 @staticmethod modifier, the static method does not need to add self, #同样可以通过类名. Method name Invocation def fun4 (): Print ("static method") if __name __== ' __main__ ': P1=people () p1.fun1 () p1.funcshow ()-People.fun3 () People.fun4 ()|
I hope this article will help you with your Python programming.