使用__slots__:
来源:互联网 发布:淘宝1688代销赚钱吗 编辑:程序博客网 时间:2024/05/17 01:52
使用__slots__:正常情况下,当我们定义了一个class,创建了一个class的实例后,我们可以给该实例绑定任何属性和方法,这就是动态语言的灵活性,先定义class:class Student(object): def __init__(self, name, score): self.name = name self.score = score def print_score(self): print '%s: %s' % (self.name, self.score -2)然后,尝试给实例绑定一个属性:from mycompany.web.Student import *s=Student('a','b')print s.names.name='Michael'print s.nameC:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.pyaMichael还可以尝试给实例绑定一个方法:class Student(object): def __init__(self, name, score): self.name = name self.score = score def print_score(self): print '%s: %s' % (self.name, self.score -2) def set_age(self, age): # 定义一个函数作为实例方法 self.age = ageclass Student(object): def __init__(self, name, score): self.name = name self.score = score def print_score(self): print '%s: %s' % (self.name, self.score -2) def set_age(self, age): self.age = age return self.agefrom mycompany.web.Student import *s=Student('a','b')print s.names.name='Michael'print s.nameprint s.set_age(77)通常情况下,上面的set_score方法可以直接定义在class中,但动态绑定允许我们在程序运行的过程中动态给class加上功能,这在静态语言中很难实现。使用__slots__但是,如果我们想要限制class的属性怎么办?比如,只允许对Student实例添加name和age属性:为了达到限制的目的,Python允许在定义class的时候,定义一个特殊的__slots__变量,来限制该class能添加的属性:class Student(object): def __init__(self, name,age, score): self.name = name self.age = age self.score = score def print_score(self): print '%s: %s' % (self.name, self.score -2,self.score)-----------------------------------------------------------------------------class Student(object): def __init__(self, name,age, score): self.name = name self.age = age self.score = score def print_score(self): print '%s: %s' % (self.name, self.score -2,self.score)from mycompany.web.Student import *s=Student('a',12,23)print s.names.name='Michael'print s.nameprint '---------------------------'print s.ages.age=34print s.ageprint '---------------------------'print s.scores.score=45print s.scoreC:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.pyaMichael---------------------------1234---------------------------2345class Student(object): __slots__ = ('name','age','score') def __init__(self, name,age, score): self.name = name self.age = age self.score = score def print_score(self): print '%s: %s' % (self.name, self.score -2,self.score)from mycompany.web.Student import *s=Student('a',12,23)print s.nameC:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.pya此时可以:
阅读全文
0 0
- 使用__slots__
- 使用__slots__
- 使用__slots__
- 使用 __slots__
- 使用__slots__
- 使用__slots__:
- python __slots__ 使用
- python使用__slots__
- __slots__的使用
- python 使用__slots__
- Python基础-使用__slots__
- __slots__
- __slots__
- python学习笔记 使用__slots__
- Python中使用__slots__注意项
- python的动态性以及 使用__slots__
- python学习——使用__slots__
- 在Python中使用__slots__方法的详细教程
- Spring Boot使用redis做数据缓存
- win10蓝屏问题,关于驱动kisSaasUrlRedirectKnl64.sys 的
- java循环拼字符串
- linkit one学习笔记-开发板简介
- Altium designer在PCB界面的高亮方法
- 使用__slots__:
- 模拟itoa和atoi
- 纯C#代码生成条码
- 奶牛身高
- 用java 自动登录一个网站的例子
- test
- libxls库的使用
- Android中EditTex焦点设置和弹不弹出输入法的问题
- 热修复技术 Sophix