python 面向对象六 动态添加方法 __slots__限制动态添加方法
一、动态添加属性
>>> class Student(object):
pass >>> st = Student()
>>> st.name = 'Jack'
>>> st.name
'Jack'
二、动态给实例添加方法
>>> from types import MethodType
>>> class Student(object):
pass >>> def set_age(self, age):
self.age = age >>> st.set_age = MethodType(set_age, st) # 给实例绑定一个方法
>>> st.set_age(25)
>>> st.age
25
三、动态给类添加方法
>>> class Student(object):
pass >>> def set_score(self, score):
self.score = score >>> Student.set_score = set_score
>>> s1 = Student()
>>> s1.set_score(100)
>>> s1.score
100
>>> s2 = Student()
>>> s2.set_score(50)
>>> s2.score
50
四、限制实例的属性 __slots__
>>> class Student(object):
__slots__ = ('name', 'age') # 用tuple定义允许绑定对的属性名称 >>> s = Student()
>>> s.name = 'Jack'
>>> s.age = 20
>>> s.score = 100
Traceback (most recent call last):
File "<pyshell#53>", line 1, in <module>
s.score = 100
AttributeError: 'Student' object has no attribute 'score'
__slots__定义的属性仅对当前类实例起作用,对继承的子类是不起作用的:
>>> class GraduateStudent(Student):
... pass
...
>>> g = GraduateStudent()
>>> g.score = 9999
除非在子类中也定义__slots__,这样,子类实例允许定义的属性就是自身的__slots__加上父类的__slots__:
>>> class GraduateStudent(Student):
__slots__ = ('score', ) >>> g = GraduateStudent()
>>> g.score = 100
>>> g.name = 'Mike'
>>> g.age = 10
>>> g.other = 'abc'
Traceback (most recent call last):
File "<pyshell#69>", line 1, in <module>
g.other = 'abc'
AttributeError: 'GraduateStudent' object has no attribute 'other'
python 面向对象六 动态添加方法 __slots__限制动态添加方法的更多相关文章
- Python面向对象-类、实例的绑定属性、绑定方法和__slots__
绑定属性 从之前的文章中,我们知道python是动态语言——实例可以绑定任意属性. 那如果实例绑定的属性和类的属性名一样的话,会是什么情况呢? >>> class Student(o ...
- python 面向对象(成员,静态,类)的(变量,方法)区别
静态方法是使用@staticmethod装饰的方法,并且参数表中不需要self或者cls,可以访问类成员变量 默认的方法是有self的,是成员方法,可以访问私有变量和方法(通过self.xxx),或者 ...
- python 面向对象(六)MRO C3算法 super
########################总结################ 面向对象回顾 类:对某一个事物的描述,对一些属性和方法的归类 class 类名: var=123#类变量 def ...
- python 面向对象(三)类与类之间的关系 初始化方法一些类
###################总结################# 面试的时候 让写python一些特殊方法 __init__ 创建对象的时候初始化 __new__对象实例化调用第一个方法 ...
- Python面向对象 --- 类的设计和常见的内置方法
面向对象:一种基于面向过程的新的编程思想.也就是说面向对象是将功能等通过对象来实现,将功能封装进对象之中,让对象去实现具体的细节:这种思想是将数据作为第一位,而方法或者说是算法作为其次,这是对数据一种 ...
- python 面向对象六 类属性和实例属性
一.实例属性 Python是动态语言,根据类创建的实例可以任意绑定属性. >>> class Student(object): ... def __init__(self, name ...
- python面向对象学习(三)私有属性和私有方法
目录 1. 应用场景和定义方式 2. 伪私有属性和私有方法 在java或者其他的编程语言中,使用访问修饰符来限制属性和方法的访问级别,一般有public.protected.default.priva ...
- 【17】有关python面向对象编程的提高【多继承、多态、类属性、动态添加与限制添加属性与方法、@property】
一.多继承 案例1:小孩继承自爸爸,妈妈.在程序入口模块再创建实例调用执行 #father模块 class Father(object): def __init__(self,money): self ...
- Python面向对象高级编程:__slot__(给实例添加方法、属性)
纲要: 本章总的来说是给实例添加属性,给类添加方法两个主题,以及相应的作用范围.总结如下: 1.给实例添加属性(作用范围:当然是只对当前实例有效): 2.用__slots__限制可以给实例添加的属性( ...
随机推荐
- linux date 格式化时间和日期
[root@108test ~]# date -d today +"%Y-%m-%d" 2008-05-07 [root@108test ~]# date -d today + ...
- SystemInformationRequestHandlers
SystemInformationRequestHandlers - Solr Wiki Search: Solr Wiki Login SystemInformationRequestHandler ...
- HDU 6396 贪心+优先队列+读入挂
Swordsman Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- delphi操作xml学习笔记 之一 入门必读
Delphi 对XML的支持---TXMLDocument类 Delphi7 支持对XML文档的操作,可以通过TXMLDocument类来实现对XML文档的读写.可以利用TXMLDocum ...
- 2017多校Round7(hdu6120~hdu6132)
补题进度:9/13 1001 待填坑 1002(数学推导) 题意 有一个按顺序的n个点的k叉树,问每个点子树个数的异或和是多少(n,k<=1e18) 分析 可以先求出最大的d,满足d以上都是满K ...
- 报错:An error occurred at line: 22 in the generated java file The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory
org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 22 in ...
- xml解析工具mashaller javaee自带解析类
1.怎样去掉Marshaller的格式化? : JAXBContext context = JAXBContext.newInstance(Entity.class); Marshaller mars ...
- Apache 处理svg工具包Apache(tm) Batik SVG Toolkit
Apache™ Batik SVG Toolkit¶ Overview¶ Batik is a Java-based toolkit for applications or applets that ...
- python执行
转载:https://www.cnblogs.com/zflibra/p/4180796.html
- Linux下用Xdebug调试php
Linux下用Xdebug调试php 博客分类: php PHPLinuxZendEclipseC# 为了调试PHP程序,安装一下xdebug. 官方网址: http://www.xdebug.org ...