1.class 语句python
class语句细节python3.x
class语句如何获得命名空间函数
class语句通常形式 工具
## 根据上述所言,className是类对象的一个引用
class className(superclass1,superclass2,...):
''' 定义类属性,属于全部实例的共享数据,经过类语句下进行定义和建立 '''
class_attr = value
''' 定义实例方法以及实例属性 '''
def method(self,data): ## 定义实例方法
self.attr = data ## 设置实例属性,经过带有self的方法来分配属性信息复制代码
2.方法优化
实例方法对象调用等价于类方法函数调用ui
## python自动将实例方法的调用自动转成类方法函数,并传递实例对象做为第一个参数传递
class Person:
def study(self,name):
print("%s study method in for %s" % (name,self.__class__.__name__)
>>> p = Person()
>>> p.study("keithl")
keithl study method in for Person
>>> Person.study(p,"keithl")
keithl study method in for Person
## instance.method(arg1,arg2,...) == class.method(instance,arg1,arg2,...)复制代码
调用超类的构造函数
__init__
方法spa
class Person:
def __init__(self):
print("call person init ....")
class Student(Person):
pass
>>> s = Student() ## 建立子类时会调用父类构造函数,缘由是子类没有定义本身的构造函数
call person init ....
## 为子类增长构造函数
class Student(Person):
def __init__(self):
print("call student init ....")
>>> s = Student() ## 只输出子类的__init__方法,并无调用父类方法,缘由在于python是根据命名空间来执行调用方法
call student init ....
## 若要调用父类构造方法则必须显示进行调用
class Student(Person):
""" 必须在子类构造函数中显式调用父类的构造函数,并传递子类的self引用 """
def __init__(self):
print("call student init start....")
Person.__init__(self)
print("call student init end....")
>>> s = Student()
call student init start....
call person init ....
call student init end....复制代码
静态方法code
## person.py
class Person:
num = 1
""" 定义一个没有带参数的普通方法 """
def printNum():
Person.num += 1
print("the number is %s" % Person.num)
printNum = staticmethod(printNum) ## 声明为静态方法
""" 定义一个带参数的普通方法,此参数为类对象参数 """
def clsPrintNum(cls):
Person.num += 1
print("the number is %s" % Person.num)
clsPrintNum = classmethod(clsPrintNum) ## 声明为类方法
>>> Person.printNum()
the number is 2
>>> Person.clsPrintNum()
the number is 3
## person.py 使用装饰器来声明静态或类方法
class Person:
num = 1
@staticmethod
def printNum():
Person.num += 1
print("the number is %s" % Person.num)
@classmethod
def clsPrintNum(cls):
Person.num += 1
print("the number is %s" % Person.num)复制代码
静态方法、类方法与实例方法cdn
class Person:
@staticmethod
def static_method():
print("static method ...")
@classmethod
def class_method(cls):
print("class method ....")
def instance_method(self):
print("instance method ...")
''' python3.x能够调用下面的函数,能够说是静态方法,但严格意义上是属于类的一个行为方法,可是python2.x没法该方法 '''
def fn():
print("just a fn,if py3.x,it is static method")
## 总结:
1)在类中定义方法必定要规范化,明确是静态方法仍是类方法抑或是实例方法
2)避免使用最后一种方式在类中定义方法复制代码
3.命名空间与做用域对象
无点号运算的变量名称
X = "global X"
def enclosing_fn():
## global X
X = "enclosing fn" ## 建立当前enclosing_fn的本地变量X若是没有声明为全局变量的话复制代码
X = "global X"
def enclosing_fn():
X = "enclosing fn" ## 若是注释此行,将打印全局的变量X
print(X)
def local_x()
x = "local x" ## 若是仅注释此行,将会打印嵌套的变量X
print(x)
local_x()复制代码
点号的属性变量名称
>>> p = Person()
## 在对象实例的命名空间建立或更改属性名称name
p.name = "keithl" ## 并没有进行变量名称的搜索
## 在类的命名空间中建立或更改属性名称name
Person.name = "keithl" ## 并没有进行变量名称的搜索复制代码
>>> p = Person()
>>> p.name ## 从对象命名空间开始按照继承树来搜索
>>> Person.name ## 从类的命名空间开始按照继承树来搜索复制代码
命名空间字典
__dict__
来显示__class__
属性连接__bases__
属性连接,能够经过递归往上遍历超类__dict__
查看模块、类或者对象的属性信息类与模块的关系总结
类
模块
喜欢能够关注我我的公众号,持续更新工程师技术平常