Python_class, access restrictions (private property and private methods)

Another focus is the "restrict access" in the object-oriented
we are talking about a property is defined as private property, so that you add two underscores in front of the attribute names: __ xxx
private property are not freely accessible by access restrictions to protect the function

class Hero(object):
    def __init__(self,name,skill): #我们将超级英雄的名字和技能导入
        self.name = name
        self.__skill = skill   #这里,钢铁侠小心机得隐藏了自己的属性(实例变量==>私有变量)

iron = Hero('Ironman','IonCannon')
print(iron.name)  #这里我们会得到Ironman
print(Hero.skill)  #这里却得不到IonCannon

However, private property can be called class methods, such as the following show method

The above said, you can not modify the external random access calls, but in fact .... still can be accessed by get_xxx method

class Hero(object):
    def __init__(self,name,skill): #我们将超级英雄的名字和技能导入
        self.name = name
        self.__skill = skill #私有属性skill
    def get_skill(self):  #get只是一个格式,可以改其他不影响函数,只是在python习惯使用他(大家都这么用咱就不要做杠精好吧)
        return self.__skill  #这样通过实例内部调用,返回私有属性skill

iron= Hero('Ironman','IonCannon')
print(iron.get_skill())  #这样就可以打印出被隐藏的skill了~

# There are even more cruel, we also can be modified by set_xxx, emmm ... (say good private property it? (● ∀ ●))
 

class Hero(object):
    def __init__(self,name): #我们将超级英雄的名字和技能导入
        self.name = name
        self.__skill = 'Kungfu' #私有属性skill,这里要做的是修改,所以我们想给skill赋值
    def get_skill(self):  #get只是一个格式,可以改其他不影响函数,只是在python习惯使用他(大家都这么用咱就不要做杠精好吧)
        return self.__skill  #这样通过实例内部调用,返回私有属性skill
    def set_skill(self,skill_02):
        self.__skill = skill_02  #这样通过实例内部调用,返回私有属性后再将参数skill_02f赋给他

iron= Hero('Ironman')
iron.set_skill('SuperIonCannon')
print(iron.get_skill())  #看看是不是边成‘超级离子炮了’

Here, even if there is a default property __skill this way can still modify
mention that, over the format you want to install strict unwritten rules so we can write ~ enjoyable

.... Another point, we really can only be called from within it? ? ? (¯ ▽ ¯) ", No

class Hero(object):
    def __init__(self,name,skill): #我们将超级英雄的名字和技能导入
        self.name = name
        self.__skill = skill #私有属性skill
iron= Hero('Ironman','IonCannon')
print(iron._Hero__skill) #==>'IonCannon'可以通过这样的函数直接访问得到私有属性

But it is strongly recommended that you do not do it, because different versions of the Python interpreter may put __name into a different variable names. (Blog from Liao Xuefeng master)
there is a ... can no longer put up with, it's clear that good hungry can amend it? ? ?
Built-used @property decorator  

class Hero(object):
    def __init__(self,name,): #我们将超级英雄的名字和技能导入
        self.name = name
        self.__skill = 'KungFu'   
    @property                  #从这里开始
    def use_skill(self):
        return self.__skill
    @use_skill.setter
    def skill(self,skill):
        self.__skill = skill   #到这里接受

    def show(self):
        print('My name is %s, and my skill is %s'%(self.name,self.__skill))

iron = Hero('ironMan')
iron.skill = 'IonCannon'
iron.show()        

# * These are the @property and the SET / GET ~ ~ 
# Note property is fixed format

@property                  #从这里开始
def use_skill(self):       #定义一个函数获取私有属性
    return self.__skill
@use_skill.setter          #这里是固定格式,即上一个函数名加上.setter
def skill(self,skill):     #传入变量修改私有属性
    self.__skill = skill   #毫无尊严是私有属性!!!!

OK, now to summarize it, we must first pay attention to the variable name, must be __xxx, (__xxx__ should not be this, this is a cube function),
In addition, there will be some variables _xxx an underscore, so there is no private private variables attributes that we are free to access
but in fact the author is not willing to access it, so rivers and lakes comply with the rules, when we see such attributes try not to visit Van Do not modify ~~
Another point: Python really language community shame, no rules at all, but obviously private access, or can be accessed directly. . . U • S Factory • * U, and later will be learned 'duck property' ...
============================== ===============================================
Finally, introduce private methods of Python, it can neither by calling the class, nor by calling instance
and private property, like the need to get it through indirect methods example

class Hero(object):
    def __init__(self,name,skill): #我们将超级英雄的名字和技能导入
        self.name = name
        self.skill = skill
    def __hideskill(self):  #私有方法,我们假设他有个隐藏的技能,就是boooooo
        print('boooooooo')
    def get_hideskill(self): #通过一个方法得到另一个方法
        self.__hideskill()

iron = Hero('iIronman','IonCannon')
iron.get_hideskill()   #得到隐藏技能booooooo~

 

 

 


 


 

 

 

Published 14 original articles · won praise 12 · views 10000 +

Guess you like

Origin blog.csdn.net/Watson_Ashin/article/details/81586556
Recommended