Details about attributes and methods of Python programming, and details about python programming examples
This article describes how to use attributes and methods in Python programming. Share it with you for your reference. The specific analysis is as follows:
I. Attributes
In python, attributes are classified into public and private attributes. Public attributes can be called outside the class, and private attributes cannot be called outside the class. The public attribute can be any variable, and the private attribute is a variable starting with a double underline.
The following defines a People class, which has a public property name and a private property _ age.
Class People (): def _ init (self): self. name = 'zhangshan' self. _ age = 24
We create a People class instance, P1. the following error occurs when we call its private attribute _ age.
>>> P1. _ age
Traceback (most recent call last ):
File "<pyshell #7>", line 1, in <module>
P1. _ age
AttributeError: 'people' object has no attribute '_ age'
This indicates that private attributes cannot be used outside the class. To call the value of a private property, you can define a method within the class.
>>> class People():def __init__(self): self.name='jack' self.__age=23def showinfo(self): print(self.__age)>>> p2=People()>>> p2.showinfo()23
Some may ask, why can't we call the double-underline attribute outside the class? Next we will discuss the python object-oriented private mechanism.
In Python, attributes and Methods Starting with double underscores are automatically added to the name of an object after being instantiated. because the name has been changed, it is naturally impossible to access it through the name at the beginning of the dual-Hop Line, so as to achieve the inaccessibility goal.
You can use Instance name. _ dict _ to view the property set of an object.
Different from other object-oriented programming languages, Python's design philosophy is simple, so if you really want to call private properties, you can still call it.
Ii. Method
In python, methods are classified into public, private, class, and static methods.
The following is a complete example.
#! /Usr/bin/python # coding: UTF-8 _ author _ = 'mxi4oyu' class People (): def _ init _ (self): self. name = 'zhangshan' self. _ age = 23 def fun1 (self): # common methods can be called outside the class # object names can be used. method name to call print ("common method") def _ fun2 (self ): # private methods cannot be called outside the class # You can call the private method print ("Private method") def funcshow (self): self. _ fun2 () @ classmethod # Add the @ classmethod modifier to the class method. The class method can use the class name. def fun3 (self): print ("class method") @ staticmethod # The @ staticmethod modifier must be added to the static method. self is not required for static methods, # You can also use the class name. method name call 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 Python programming.