zoukankan      html  css  js  c++  java
  • Python class的属性访问控制和内建函数重写实现高级功能以及@property

    一、类属性的访问控制

    Python Class确实是博大精深,我们还是来温习一下属性的访问控制作为开(fu)场(xi)。

    首先_varname是可以访问的,__varname是不能直接访问(原理是__varname变成了_classname__varname)

     1 >>> class A:
     2 ...     _aa = 1
     3 ...     __bb = 2
     4 ...
     5 >>>
     6 >>>
     7 >>> c = A()
     8 >>> c._aa
     9 1
    10 >>> c.__bb
    11 Traceback (most recent call last):
    12   File "<stdin>", line 1, in <module>
    13 AttributeError: A instance has no attribute '__bb'
    14 >>> c._A__bb
    15 2
    16 >>>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 

    二、内建函数:

    不想啰嗦直接上代码,注释来讲解:

     1 class Person(object):
     2     def __init__(self,name):
     3             self.name = name
     4     def __str__(self):
     5             return  "<class:Person Name:%s>"%self.name
     6     def __repr__(self):
     7             return  "<Name:%s>"%self.name
     8 
     9 tom = Person("Tom")
    10 print repr(tom)            
    11 print tom

     1 class Person(object):
     2     def __init__(self,name,age):
     3         self.name = name
     4         self.age = age
     5     def __str__(self):
     6         return  "<class:Person Name:%s>"%self.name
     7     def __repr__(self):
     8         return "<Name:%s>"%self.name
     9     def __len__(self):
    10         return self.age
    11 tom = Person("Tom",20)
    12 print repr(tom)            
    13 print tom
    14 print len(tom)

    当然还有很多:

     1 def __iter__(self):#重写内建迭代功能
     2     ret = do_something()
     3     return ret  
     4 
     5 def __getitem__(self):#增加下表取对象功能
     6     pass
     7 
     8 def __getattr__(self):#增加取自身属性的处理
     9     pass
    10 
    11 def __call__(self):#直接实例当做函数调用
    12     pass
    13 """
    14 class A:
    15     def __call__(self):
    16          print "ok"       
    17 a = A()
    18 a()
    19 >>> ok
    20 """

    三、@property方法属性化:

     1 class student:
     2     def __init__(self,name):
     3         self.name = name
     4     @property
     5     def age(self):
     6         return self.age
     7     @age.setter
     8     def age(self,age):
     9         self.age = age
    10 
    11 """
    12 a = student("stu")
    13 a.age = 20
    14 a.age
    15 >>> 20
    16 """
  • 相关阅读:
    nginx 禁止访问某个目录
    ssh 无法链接的解决思路
    linux 时间修改(同步到时间服务器)
    BitNami Gitlab的重启问题
    gitlab push时报错
    VirtualBox 安装centos后无法上网
    VirtualBox 创建64位系统的问题
    NOIP2010 题解
    NOIP2011 题解
    NOIP2012 题解
  • 原文地址:https://www.cnblogs.com/KevinGeorge/p/8137094.html
Copyright © 2011-2022 走看看