​ 通常情况下,当我们定义了一个class,创建了一个class的实例后,我们可以给该实例绑定任何属性和方法,这就是动态语言的灵活性。下来我就讲下添加属性和方法,同时也将下限值添加属性方法。

添加属性

​ 给一个实例添加属性和方法时,只有对象能使用,对类添加方法和属性时,为类属性和类方法

>>> class Peopre(object):
"""docstring for Peopre"""
def __init__(self):
pass ... ... ... ... ...
>>> p=Peopre()
>>> Peopre.__dict__
mappingproxy({'__module__': '__main__', '__doc__': 'docstring for Peopre', '__init__': <function Peopre.__init__ at 0x02B1B6F0>, '__dict__': <attribute '__dict__' of 'Peopre' objects>, '__weakref__': <attribute '__weakref__' of 'Peopre' objects>})
>>> p.__dict__
{} # -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
# 对实例添加属性
>>> p.name='张三'
>>> p.name
'张三'
>>> Peopre.__dict__
mappingproxy({'__module__': '__main__', '__doc__': 'docstring for Peopre', '__init__': <function Peopre.__init__ at 0x02B1B6F0>, '__dict__': <attribute '__dict__' of 'Peopre' objects>, '__weakref__': <attribute '__weakref__' of 'Peopre' objects>}) # -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
# 对类添加属性,添加的为类属性
>>> Peopre.name='name'
>>> Peopre.age='12'
>>> Peopre.__dict__
mappingproxy({'__module__': '__main__', '__doc__': 'docstring for Peopre', '__init__': <function Peopre.__init__ at 0x02B1B6F0>, '__dict__': <attribute '__dict__' of 'Peopre' objects>, '__weakref__': <attribute '__weakref__' of 'Peopre' objects>, 'name': 'name', 'age': '12'}) >>> p1=Peopre()
>>> p1.name
'name'
>>> p1.age
'12'
>>> p.name
'张三'

添加方法

>>> class Peopre(object):
"""docstring for Peopre"""
def __init__(self):
pass
... ... ... ...
>>> def hi():
print("你好")
... ...
>>> Peopre.__dict__
mappingproxy({'__module__': '__main__', '__doc__': 'docstring for Peopre', '__init__': <function Peopre.__init__ at 0x0327E810>, '__dict__': <attribute '__dict__' of 'Peopre' objects>, '__weakref__': <attribute '__weakref__' of 'Peopre' objects>}) # -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
# 添加的方法为类方法
>>> Peopre.hi=hi
>>> Peopre.__dict__
mappingproxy({'__module__': '__main__', '__doc__': 'docstring for Peopre', '__init__': <function Peopre.__init__ at 0x0327E810>, '__dict__': <attribute '__dict__' of 'Peopre' objects>, '__weakref__': <attribute '__weakref__' of 'Peopre' objects>, 'hi': <function hi at 0x0327E858>})
>>> Peopre.hi
<function hi at 0x0327E858>
>>> Peopre.hi()
你好
>>> def hello():
... print("hello!")
...
>>> p = Peopre()
>>> p.hi
<bound method hi of <__main__.Peopre object at 0x032722B0>>
>>> p.hi()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: hi() takes 0 positional arguments but 1 was given # -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
# 添加的为普通方法
>>> p.hello = hello
>>> p.hello()
hello!
>>> p.__dict__
{'hello': <function hello at 0x0327E8A0>}
>>>

删除属性、方法

删除的方法:

  • del 对象.属性名
  • delattr(对象, "属性名")
>>> class Peopre(object):
"""docstring for Peopre"""
def __init__(self, name):
self.name = name
def hi(self):
print("我的名字是:%s"%self.name)
... ... ... ... ... ...
>>> p = Peopre("张三")
>>> p.hi()
我的名字是:张三
>>> p.__dict__
{'name': '张三'}
>>> del(p.name)
>>> p.__dict__
{}
# 删除方法
>>> Peopre.__dict__
mappingproxy({'__module__': '__main__', '__doc__': 'docstring for Peopre', '__init__': <function Peopre.__init__ at 0x02D2E858>, 'hi': <function Peopre.hi at 0x02D2E810>, '__dict__': <attribute '__dict__' of 'Peopre' objects>, '__weakref__': <attribute '__weakref__' of 'Peopre' objects>})
>>> def hello():
... print("你好")
...
>>> Peopre.hello = hello
>>> Peopre.__dict__
mappingproxy({'__module__': '__main__', '__doc__': 'docstring for Peopre', '__init__': <function Peopre.__init__ at 0x02D2E858>, 'hi': <function Peopre.hi at 0x02D2E810>, '__dict__': <attribute '__dict__' of 'Peopre' objects>, '__weakref__': <attribute '__weakref__' of 'Peopre' objects>, 'hello': <function hello at 0x0110D660>})
>>> del(Peopre.hello)
>>> Peopre.__dict__
mappingproxy({'__module__': '__main__', '__doc__': 'docstring for Peopre', '__init__': <function Peopre.__init__ at 0x02D2E858>, 'hi': <function Peopre.hi at 0x02D2E810>, '__dict__': <attribute '__dict__' of 'Peopre' objects>, '__weakref__': <attribute '__weakref__' of 'Peopre' objects>})
>>>

__slots__

​ 限制该class能添加的属性. 但__slots__定义的属性仅对当前类起作用,对继承的子类是不起作用的.

>>> class Peopre(object):
"""docstring for Peopre"""
__slots__ = ("name","age")
... ... ...
>>> p = Peopre
>>> p = Peopre()
>>> p.name= "张三"
>>> p.age= 14
>>> p.height = 170
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Peopre' object has no attribute 'height'
# 对继承的子类是不起作用的
>>> class Test(Peopre):
pass ... ... ...
>>> t = Test()
>>> t.height = 170
>>>

最新文章

  1. 一篇很好的Java、C、PHP、前端、Android、IOS的文章
  2. Linux学习笔记(7)-进程
  3. Asp.Net Core-几行代码解决Razor中的嵌套if语句
  4. java高新技术-类加载器
  5. H5 App开发用WeX5垃圾 试用一周,我果断放弃了wex5
  6. Protocol Buffer多态
  7. subplot的几个详细说明
  8. 细节!重点!易错点!--面试java基础篇(一)
  9. Sql Server 查询多行并一行
  10. BOM中的各种height
  11. 利用JS去做响应式布局
  12. [C# 基础知识系列]专题七: 泛型深入理解(一) (转载)
  13. MAC OSX Xcode硬盘清理
  14. 《linux就该这么学》开课,linux之路新开始
  15. [k8s]k8s配置nfs做后端存储&amp;配置多nginx共享存储&amp;&amp;statefulset配置
  16. loopback 代码解析
  17. spring基础----&gt;spring自定义初始化(二)
  18. Sublime Text 2 - 性感无比的代码编辑器!程序员必备神器!
  19. linux 中 iptables关于ping的问题
  20. [CF1027F]Session in BSU[最小基环树森林]

热门文章

  1. python图片云
  2. navicat执行大容量的.sql文件时的设置
  3. poj 2774 后缀数组 两个字符串的最长公共子串
  4. R语言计算moran‘I
  5. node.js如何让前端请求时能跨域
  6. 一篇很棒的 MySQL 触发器学习教程
  7. CF&amp;&amp;CC百套计划2 CodeChef December Challenge 2017 Penalty Shoot-out
  8. UNDERSTANDING THE GAUSSIAN DISTRIBUTION
  9. centos无法通过ssh连接的解决
  10. soj1762.排座椅