#coding:utf-8
"""
一段小代码说明@property装饰器的用法、__name是私有变量
外部不能通过foo.__name访问,但可以通过foo._Foo__name访问。
如果想通过点运算符设置和访问实例属性怎么办,@property派上用途了,
先将一个方法头上加上@property,这个方法就变成实例属性了,再加上这个@get_name.setter
装饰器(@属性名.setter)就可以通过赋值来设置属性。
python北京周末培训班
https://github.com/pythonpeixun/article/blob/master/beijing_weekend.md
python上海周末培训班
https://github.com/pythonpeixun/article/blob/master/shanghai_weekend.md
python远程视频培训
https://github.com/pythonpeixun/article/blob/master/index.md
c语言从入门到精通远程视频培训
https://github.com/pythonpeixun/article/blob/master/c_course.md
咨询:qq:1465376564 黄哥python培训
"""
class Foo(object):
def __init__(self, name, professional):
self.__name = name
self.professional = professional
@property
def get_name(self):
return self.__name
@get_name.setter
def get_name(self, another_name):
self.__name = another_name
name = ‘柴静‘
professional = ‘新闻人‘
foo = Foo(name, professional)
print foo.get_name
print foo._Foo__name
foo.get_name = ‘雾霾女神‘
print foo.get_name
print Foo.get_name