一、super()
类
单继承中,主要是用来调用父类的方法的
class A:
def __init__(self):
self.n = 2
def add(self, m):
print('self is {0} @A.add'.format(self))
self.n += m
class B(A):
def __init__(self):
self.n = 3
def add(self, m):
print('self is {0} @B.add'.format(self))
super().add(m)
self.n += 3
if __name__ == '__main__':
b = B()
b.add(2)
print(b.n)
self is <__main__.B object at 0x106c49b38> @B.add
self is <__main__.B object at 0x106c49b38> @A.add
8
多继承中
class A:
def __init__(self):
self.n = 2
def add(self, m):
print('self is {0} @A.add'.format(self))
self.n += m
class B(A):
def __init__(self):
self.n = 3
def add(self, m):
print('self is {0} @B.add'.format(self))
super().add(m)
self.n += 3
class C(A):
def __init__(self):
self.n = 4
def add(self, m):
print('self is {0} @C.add'.format(self))
super().add(m)
self.n += 4
class D(B, C):
def __init__(self):
self.n = 5
def add(self, m):
print('self is {0} @D.add'.format(self))
super().add(m)
self.n += 5
if __name__ == '__main__':
d = D()
d.add(2)
print(d.n)
self is <__main__.D object at 0x10ce10e48> @D.add
self is <__main__.D object at 0x10ce10e48> @B.add
self is <__main__.D object at 0x10ce10e48> @C.add
self is <__main__.D object at 0x10ce10e48> @A.add
19
二、__init__()
,被称为类的构造函数或初始化方法
该方法是一个特殊的类实例方法,构造方法用于创建对象时使用,每当创建一个类的实例对象,Python 解释器都会自动调用它。
__init__()
方法可以包含多个参数,但必须包含一个名为 self 的参数,且必须作为第一个参数
class Employee:
'所有员工的基类'
empCount = 0
def __init__(self, name, salary):
self.name = name
self.salary = salary
Employee.empCount += 1
def displayCount(self):
print("Total Employee %d" % Employee.empCount)
def displayEmployee(self):
print("Name : ", self.name, ", Salary: ", self.salary)
if __name__ == '__main__':
"创建 Employee 类的第一个对象"
emp1 = Employee("Zara", 2000)
"创建 Employee 类的第二个对象"
emp2 = Employee("Manni", 5000)
emp1.displayEmployee()
emp2.displayEmployee()
print("Total Employee %d" % Employee.empCount)
Name : Zara , Salary: 2000
Name : Manni , Salary: 5000
Total Employee 2
三、strftime()
方法
按目标格式输出时间
from datetime import datetime
now = datetime.now() # current date and time
year = now.strftime("%Y")
print("year:", year)
month = now.strftime("%m")
print("month:", month)
day = now.strftime("%d")
print("day:", day)
time = now.strftime("%H:%M:%S")
print("time:", time)
_time = now
print(_time)
date_time = now.strftime("%Y-%m-%d, %H:%M:%S")
print("date and time:",date_time)
year: 2021
month: 12
day: 08
time: 15:56:32
2021-12-08 15:56:32.479174
date and time: 2021-12-08, 15:56:32
四、__all__
变量
该变量的值是一个列表,存储的是当前模块中一些成员(变量、函数或者类)的名称。
通过在模块文件中设置 __all__
变量,当其它文件以“from 模块名 import *”的形式导入该模块时,该文件中只能使用 __all__
列表中指定的成员。
#demo.py
def say():
print("人生苦短,我学Python!")
def CLanguage():
print("C语言中文网:http://c.biancheng.net")
def disPython():
print("Python教程:http://c.biancheng.net/python")
__all__ = ["say"]
#test.py
import demo
demo.say()
demo.CLanguage()
demo.disPython()
人生苦短,我学Python!
C语言中文网:http://c.biancheng.net
Python教程:http://c.biancheng.net/python
#demo.py
def say():
print("人生苦短,我学Python!")
def CLanguage():
print("C语言中文网:http://c.biancheng.net")
def disPython():
print("Python教程:http://c.biancheng.net/python")
__all__ = ["say"]
#test.py
from demo import *
say()
CLanguage()
disPython()
人生苦短,我学Python!
C语言中文网:http://c.biancheng.net
Traceback (most recent call last):
File "C:/Users/mengma/Desktop/2.py", line 4, in <module>
disPython()
NameError: name 'disPython' is not defined