python面向对象(七)属性方法的添加
通常情况下,当我们定义了一个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
>>>
python面向对象(七)属性方法的添加的更多相关文章
- python面向对象基础-属性/方法
- python 面向对象、特殊方法与多范式、对象的属性及与其他语言的差异
1.python 面向对象 文章内容摘自:http://www.cnblogs.com/vamei/archive/2012/06/02/2532018.html 1.__init__() 创建对 ...
- Python面向对象之内置方法
1.isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls 的对象 issubclass(sub, s ...
- python静态方法类方法属性方法
Python的静态方法和类成员方法都可以被类或实例访问,两者概念不容易理清,但还是有区别的: 1)静态方法无需传入self参数,类成员方法需传入代表本类的cls参数: 2)从第1条,静态方法是无法访问 ...
- python 面向对象 私有属性
__init__构造函数 self.name = name # 属性, 实例变量,成员变量,字段 def sayhi()# 方法, 动态属性 私有属性不对外看到 前面加上__ class role() ...
- python面向对象之类属性,实例属性
python中的属性分为类属性和实例属性,之前已经说过一些,这里主要是对类属性与实例属性的增删改查 首先是对类属性的增删改查,下面这个是对类属性的修改,在书写类时,已经对类属性occupation进行 ...
- python面向对象内置方法关于属性篇
1.关于__xxxattr__之__getattr__.__setattr__.__delattr__ 2.关于__xxxitem__之__getitem__.__setitem__.__delite ...
- python -- 面向对象编程(属性、方法)
一.属性 对象的属性(attribute)也叫做数据成员(data member). 如果想指向某个对象的属性,可以使用格式: object.attribute 属性又分为:私有属性和公有属性. 私有 ...
- Python面向对象之魔术方法
__str__ 改变对象的字符串显示.可以理解为使用print函数打印一个对象时,会自动调用对象的__str__方法 class Student: def __init__(self, name, a ...
随机推荐
- Alpha 冲刺 —— 十分之六
队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭鸭鸭鸭鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作 测试服务器并行能力 学习MSI.CUDA ...
- 一步步创建第一个Docker App —— 1. 背景介绍
原文:https://docs.docker.com/engine/getstarted-voting-app/#/docker-stacks-and-services 你将会学习什么 本文创建 ...
- 20135319zl软件破解报告
编写一个简单的C程序.要求只有输入a,才能通过. 现在,使用objdump –d po反汇编这个程序 找到main函数,可以发现movb $0x61,0x1f(%esp)这句语句中是将字符a(对应0x ...
- Android Studio下“Error:Could not find com.android.tools.build:gradle:2.2.1”的解决方法
ref from: Android Studio下“Error:Could not find com.android.tools.build:gradle:2.2.1”的解决方法http://blog ...
- 解题:HAOI 2012 道路
题面 这题不开O2怎么过=.= 可能这种有关最短路的计数题做多了就有些感觉了...... 以每个点为基准跑出一张最短路图,然后对每个边$(u,v)$统计两个东西.一个$pre[u]$表示到达$u$这个 ...
- SpringMVC 使用@ResponseBody返回json 中文乱码
这确实是个蛋疼的问题,Spring中解析字符串的转换器默认编码居然是ISO-8859-1 既然找到问题了,那就必须想办法改过来,不同版本的Spring好像方法还不一样,网上不少说的都是Spring3. ...
- for,while,do while
long i; ;i<;i++) printf( printf("%ld\n",i); ) printf("b\n"); i=; do { printf( ...
- Codeforces Round #552 (Div. 3) 题解
Codeforces Round #552 (Div. 3) 题目链接 A. Restoring Three Numbers 给出 \(a+b\),\(b+c\),\(a+c\) 以及 \(a+b+c ...
- TIME_WAIT状态的一些总结
前言: TCP断开连接的四次握手中, 主动关闭连接的一方的TIME_WAIT状态尤为重要. 1:TCP连接的三次握手和断开的四次挥手 2:由上图可知 在主动关闭的一方, 会经历TIME_WAIT状态, ...
- mysql 对应 binlog 查看
什么是 binlog binlog,即二进制日志,它记录了数据库上的所有改变. 改变数据库的SQL语句执行结束时,将在binlog的末尾写入一条记录,同时通知语句解析器,语句执行完毕. binlog格 ...