The study of access restriction in Python(20170920) 访问限制
来源:互联网 发布:数据库建表 编辑:程序博客网 时间:2024/05/17 04:25
The study of access restriction in Python(20170920) 访问限制
class per_info(object):
def init(self, name, age, gender):
self.__name = name
self.__age = age
self.__gender = gender
# the three parameters are private parameters
# only can be get in inner
# can’t be get on outer
def show_info(self):
return (‘%s:%d,%s’ % (self.__name, self.__age, self.__gender))
how to get the private parameters
def get_name_age_gender(self): return self.__name, self.__age, self.__gender
how to modify the private parameters
def modify_parameters(self, name, age, gender): self.__name = name self.__age = age self.__gender = gender return self.__name, self.__age, self.__gender
person_1 = per_info(‘yuhanyu’, 22, ‘male’)
print(person_1.show_info()) # True
print(person_1.__name) # False
print(person_1.get_name_age_gender()) # True
print(person_1.modify_parameters(‘amu’, 23, ‘female’))
how to get the Instance variable(实例变量)
print(person_1._per_info__name)
but this way should not often use
需要注意的是,在Python中,变量名类似xxx的,也就是以双下划线开头,并且以双下划线结尾的,是特殊变量,特殊变量是可以直接访问的,不是private变量,所以,不能用name、score这样的变量名
有些时候,你会看到以一个下划线开头的实例变量名,比如_name,这样的实例变量外部是可以访问的,但是,按照约定俗成的规定,当你看到这样的变量时,意思就是,“虽然我可以被访问,但是,请把我视为私有变量,不要随意访问”
阅读全文
0 0
- The study of access restriction in Python(20170920) 访问限制
- The study of loop in Python(20170906)
- The study of slice in Python(20170911)
- The study of literation in Python(20170911)
- The study of generator in Python(20170912)
- The study of parameter of function in Python(20170908)
- Eclipse 出现Access restriction(访问jar包限制)错误
- The study of calling fuction in Python(20170907)
- The study of recursive function in Python(20170910)
- The study of List Comprehensions in Python(20170911)
- The study of iterator in Python(20170912)
- The study of Higher-order function in Python(20170913)
- The study of map() and reduce() function in Python(20170913)
- The study of Class and Instance in Python(20170918)
- Access restriction: The type 'BASE64Encoder'
- Access restriction在myeclipse中遇到的访问限制的问题
- The study of list ,tuple and conditional statement in Python(20170905)
- The study of how to define a function in Python(20170907)
- hdu 5446 Unknown Treasure lucas+CRT || 扩展lucas
- dlib 10 dlib自带demo GoogLeNet (inception)
- TCP协议的3次握手以及4次挥手
- 有趣的网站-第二弹
- [RK3288][Android6.0] 调试笔记 --- Recovery执行脚本方法
- The study of access restriction in Python(20170920) 访问限制
- 接口和抽象类的区别
- 【例子驱动学习】unique_ptr
- 鼠标划入显示提示框
- JavaScript基础之Node.js与http
- Codeforces 862C Mahmoud and Ehab and the xor
- C++标准库学习(三)--队列和string类
- Axure游戏原型设计实例应用(一)——Einsphoton
- LINUX下查看CPU和内存的使用情况