Python classes and functions (private variables, private methods, variable scope, inheritance and polymorphism)

Python class

1. Private variables and private methods in Python classes:

In Python, you can define an attribute as a private attribute by adding a double underscore before the attribute variable name

Special variable naming:
Insert picture description here
4. Private variables and attributes are only used in their own classes and cannot be called in instantiated objects;
protected types can be instantiated or modified

# 私有变量和属性只在自己的类中使用在实例化对象中不能调用
# 受保护类型的则可以实例化调用也可以修改
class Pub():
    _name = 'protected类型的变量'
    __info = '私有类型的变量'
    def _func(self):
        print("这是一个protected类型的方法")
        print(self._name)  # changed 实例化后可被修改
    def __func2(self):
        print('这是一个私有类型的方法')
    def get(self):
        return(self.__info)

p = Pub()
p._name = 'changed'  # 可修改
print(p._name)   # protected类型的变量
# print(p.__info)  # 报错'Pub' object has no attribute '__info'

p._func()        # 这是一个protected类型的方法
# p.__func2()      # 报错 'Pub' object has no attribute '__func2'
print(p._Pub__info)     # 私有类型的变量
print(p._Pub__func2())  # 这是一个私有类型的方法

For the above methods of accessing private variables and private methods reporting errors, you can access it like this:

print(p._Pub__info)     # 私有类型的变量
print(p._Pub__func2())  # 这是一个私有类型的方法

5. The private variables of init cannot be modified in the instantiated object, the protected variables of init can be modified in the instantiated object; other common attributes can be modified at will, only for the instantiated object (the value of the new instantiated object is not influences)

# init的私有变量在实例化对象中不能修改
# init的受保护变量在实例化对象中可以修改
# 其它普通属性可以任意修改,只对于本次实例化的对象(新的实例化对象值不影响)
class A:
	def __init__(self, a, b, c):
		self.a = a
		self.__b = b
		self._c = c

	def func(self):
		print(self.a, self.__b, self._c)

s = A(1, 2, 3)
s.func()  # 1 2 3
s.__b = 4
s.func()  # 1 2 3  无法修改私有属性
s._c = 4
s.func()  # 1 2 4  可修改受保护属性

two. Inheritance and polymorphism

1. Inheritance can get all the properties and methods of
the parent class. 2. You can modify and add new functions on the basis of the parent class method.
3. Polymorphism: variable data types

The principle of "opening and closing":
open for extension: allow new Animal subclasses;
closed for modification: no need to modify func() and other functions that depend on the Animal type

# 父类Animal()  子类 Dog()
# 使用isinstance()方法判断数据类型
a = Animal() # Animall类型
b = Dog()    # Dog()类型
c = list()   # list类型
print(isinstance(b, Animal))  # True
print(isinstance(a, Dog))     # False
print(isinstance(b, Dog))     # True
# 在继承关系中,如果一个实例的数据类型是某个子类,那它的数据类型也可以被看做是父类
# 但是,反过来就不行

three. Variable scope of classes and functions:

Function local variables cannot be changed outside:

# 函数局部变量外部不可改变
# 局部变量作用域只在本函数
def func():
	a = 1
	_b = 2
	__c = 3
	print(a, _b, __c)
	def func2():
		_b = 5
		__c = 6
		print(_b, __c)
	func2()
func() # 1 2 3  5 6
a = 2
func() # 1 2 3  5 6
_b = 2
func() # 1 2 3  5 6

The local variables of the class can be accessed but cannot be modified:
the protected variables of the class can be accessed but cannot be modified
. Private variables of the class cannot be accessed. To access, add the format a._A__sex and cannot be modified.

class A:
	name = 'wang'
	_age = 22
	__sex = 'men'

a = A()
print(a.name)   # wang
a.name = 'liu'  # 其实就是定义了一个新变量a.name,赋值为 'liu'
print(a.name)   # liu
print(A.name)   # wang
print(a._age)   # 22
# print(a.__sex)  # 报错没有属性 'A' object has no attribute '__sex'
print(a._A__sex)# 'men' a._A__sex 可以得到类的私有变量
def func():
	x = 1
	y = 2
	def sums(x, y):
		print(x, y)
	sums(x, y)

func()  # 1 2

1. Internal functions can use external function variables
. 2. Local global cannot change global variables.
3. Nonlocal (only for local variables): the upper-level local variables are declared, and only the nearest upper-level variable can be found. If you don't have it, you will find the upper level, but you will not find the overall situation.

Guess you like

Origin blog.csdn.net/weixin_42563968/article/details/108549432