zoukankan      html  css  js  c++  java
  • 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
    >>> 
    
  • 相关阅读:
    python3-使用进程方式进行并发请求
    matplotlib--添加图例和注解
    matplotlib修改坐标轴刻度值,刻度个数
    matplotlib(一)
    python-jit(提高代码运行速度)
    pandans处理Excel
    base64编解码实现
    wireshark使用
    python 教程
    linux命令之 top, free,ps
  • 原文地址:https://www.cnblogs.com/yangliguo/p/8158674.html
Copyright © 2011-2022 走看看