实例的变量名如果以__开头,就变成了一个私有变量(private),只有内部可以访问,外部不能访问
class MyObject(object):
def __init__(self, name, score):
self.__name = name
self.__name = score
几个方法:dir(), hasattr(),setattr(),getattr(),isinstance()
dir("abc") # 列出str类所有的方法属性
hasattr(MyObjcet,name) # 返回True or Not
getattr(MyObject,name,400)# 如果没有则返回 default(400)
setattr(MyObject,name,"jack")
isinstance(my, MyObject) # 判断class的类型
Student.set_score = set_score
class Teacher(objcet):
__slots__ = ('name','age')
s.name = "jack"
s.age = 29
s.sex = "man" # attribute error cause sex is not in __slots__
class Student(object):
@property
def width(self):
return self._width
@width.setter
def width(self,value):
if not isinstance(value,int):
raise ValueError("width must integer")
self.width = value
class MytcpServer(TCPServer,CoroutineMixIn):
pass
# MytcpServer 同时拥有两个类的方法和属性