跳至主要内容

python 类class

    python 中的基本数据类型一次仅指一种数据。

    如果您可以声明一种本身包含多个数据类型并且可以在任何函数的帮助下使用它们的数据类型,那会怎样?Python 类class为您提供了这个机会。

“name”和“age”是“Person”类的两个成员变量。每次我们声明这个类的一个对象时,它都会包含这两个变量作为它的成员。这部分是可选的,因为它们可以由构造函数初始化。

Python 类构造函数是创建类的新对象时要执行的第一段代码
构造函数可用于将值放入成员变量中。您还可以在构造函数中打印消息以确认对象是否已创建。
一旦我们了解了 python 继承,我们将了解构造函数的更大作用。构造函数方法以 def __init__ 开头。之后,第一个参数必须是“self”,因为它传递对类本身实例的引用。您还可以添加其他参数,如示例中所示的方式。'person_name' 和 'person_age' 是要创建新对象时发送的两个参数。
 
在python中创建对象的方式非常简单。首先,您输入新对象的名称,后跟赋值运算符和带参数的类名称(在构造函数中定义)。请记住,参数的数量和类型应与构造函数中接收到的参数兼容。
创建对象后,可以调用成员方法并访问成员属性(前提是它们是可访问的)。






  1. # definition of the superclass starts here  
  2. class Person:  
  3.     # initializing the variables  
  4.     name = ""  
  5.     age = 0  
  6.     #defining constructor  
  7.     def __init__(self, person_name, person_age):
  8.         self.name = person_name
  9.         self.age = person_age
  10.  
  11.     # defining class methods
  12.     def show_name(self):
  13.         print(self.name)
  14.  
  15.     def show_age(self):
  16.         print(self.age)
  17. # end of superclass definition  

  18. #definition of subclass starts here
  19. class Student(Person): # Person is the  superclass and Student is the subclass
  20.     studentId = ""
  21.    
  22.     def __init__(self, student_name, student_age, student_id):
  23.         # Calling the superclass constructor and sending values of attributes.
  24. # Python super() 函数允许我们隐式地引用超类
  25.         # 等同于下行 super().__init__(student_name, student_age)
  26.         Person.__init__(self, student_name, student_age)
  27.         self.student_id = student_id  
  28.  
  29.     def get_id(self):  
  30.         return self.student_id  #returns the value of student id
  31. #end of subclass definition
  32.  
  33. # Create an object of the superclass
  34. person1 = Person("Richard", 23)
  35. # call member methods of the objects
  36. person1.show_age()
  37. # Create an object of the subclass
  38. student1 = Student("Max", 22, "102")
  39. print(student1.get_id())
  40. student1.show_name()
  41.  


在多级继承的情况下,它会引用哪个类?好吧,Python super() 将始终引用直接超类
从输出中我们可以清楚地看到,首先__init__()调用了类 C的函数,然后是类 B,然后是类 A。调用sub_method()函数也发生了类似的事情。

  1. class A:
  2.     def __init__(self):
  3.         print('Initializing: class A')
  4.  
  5.     def sub_method(self, b):
  6.         print('Printing from class A:', b)
  7.  
  8.  
  9. class B(A):
  10.     def __init__(self):
  11.         print('Initializing: class B')
  12.         super().__init__()
  13.  
  14.     def sub_method(self, b):
  15.         print('Printing from class B:', b)
  16.         super().sub_method(b + 1)
  17.  
  18.  
  19. class C(B):
  20.     def __init__(self):
  21.         print('Initializing: class C')
  22.         super().__init__()
  23.  
  24.     def sub_method(self, b):
  25.         print('Printing from class C:', b)
  26.         super().sub_method(b + 1)
  27.  
  28.  
  29. if __name__ == '__main__':
  30.     c = C()
  31.     c.sub_method(1)
  32.  
  33. # Initializing: class C
  34. # Initializing: class B
  35. # Initializing: class A
  36. # Printing from class C: 1
  37. # Printing from class B: 2
  38. # Printing from class A: 3
  39.  

使用python多重继承解决冲突

  1.      
  2. #definition of the class starts here  
  3. class Person:  
  4.     #defining constructor  
  5.     def __init__(self, personName, personAge):  
  6.         self.name = personName  
  7.         self.age = personAge  
  8.  
  9.     #defining class methods  
  10.     def showName(self):  
  11.         print(self.name)  
  12.  
  13.     def showAge(self):  
  14.         print(self.age)  
  15.  
  16.     #end of class definition  
  17.  
  18. # defining another class  
  19. class Student: # Person is the  
  20.     def __init__(self, studentId):  
  21.         self.studentId = studentId  
  22.  
  23.     def getId(self):  
  24.         return self.studentId  
  25.  
  26.  
  27. class Resident(Person, Student): # extends both Person and Student class  
  28.     def __init__(self, name, age, id):  
  29.         Person.__init__(self, name, age)  
  30.         Student.__init__(self, id)  
  31.  
  32.  
  33. # Create an object of the subclass  
  34. resident1 = Resident('John', 30, '102')  
  35. resident1.showName()  
  36. print(resident1.getId())  
  37. # John
  38. # 102
  39.  
  40. # 使用python多重继承解决冲突
  41. class A:  
  42.     def __init__(self):  
  43.         self.name = 'John'  
  44.         self.age = 23  
  45.  
  46.     def getName(self):  
  47.         return self.name  
  48.  
  49.  
  50. class B:  
  51.     def __init__(self):  
  52.         self.name = 'Richard'  
  53.         self.id = '32'  
  54.  
  55.     def getName(self):  
  56.         return self.name  
  57.  
  58.  
  59. class C(A, B):  
  60.     def __init__(self):  
  61.         A.__init__(self)  
  62.         B.__init__(self)  
  63.  
  64.     def getName(self):  
  65.         return self.name  
  66.  
  67. C1 = C()  
  68. print(C1.getName())
  69. # 打印出来的名字是“Richard”而不是“John”。
  70. # 让我们试着了解这里发生了什么。在 C 的构造函数中,调用的第一个构造函数是 A 的构造函数。因此,C 中 name 的值变为与 A 中 name 的值相同。
  71. # 但之后,当调用 B 的构造函数时,name 的值C 中的名称被 B 中的 name 值覆盖。
  72. # 因此,C 的 name 属性在打印时保留值 'Richard'。即使我们将 C 类声明为:Class C(B, A)
  73. # 层次结构完全取决于子类中 __init__() 调用的顺序。为了完美地处理它,有一个名为 MRO(方法解析顺序)的协议
  74.  
  75.  
  76.        
  77. class A:  
  78.     def __init__(self):  
  79.         super().__init__()  
  80.         self.name = 'John'  
  81.         self.age = 23  
  82.  
  83.     def getName(self):  
  84.         return self.name  
  85.  
  86.  
  87. class B:  
  88.     def __init__(self):  
  89.         super().__init__()  
  90.         self.name = 'Richard'  
  91.         self.id = '32'  
  92.  
  93.     def getName(self):  
  94.         return self.name  
  95.  
  96.  
  97. class C(A, B):  
  98.     def __init__(self):  
  99.         super().__init__()  
  100.  
  101.     def getName(self):  
  102.         return self.name  
  103.  
  104. C1 = C()  
  105. print(C1.getName())  
  106.  
  107.  
  108. # class C(A, B):打印出来的名字是“John”。
  109. # MRO 以从左到右的方式深度工作。__init__ 方法中的 super() 表示下一个层次结构中的类。
  110. # 首先C的super()表示A。然后A的构造函数中的super寻找它的超类。如果没有找到,则执行其余代码并返回。
  111. # 所以这里调用构造函数的顺序是:C -> A -> B
  112. # 如果我们调用print(C.__mro__),那么我们就可以看到MRO的traceroute了。
  113. # (<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>)
  114. # 一旦调用了 A 的构造函数并访问了属性“name”,它就不会访问 B 中的属性“name”。为了使用 python 多重继承,必须更好地理解 MRO。
  115. # class C(B, A):打印出来的名字是“Richard”。调用print(C.__mro__)
  116. # (<class '__main__.C'>, <class '__main__.B'>, <class '__main__.A'>, <class 'object'>)
  117.  

评论

此博客中的热门博文

Mongo 入门

https://pymongo.readthedocs.io/en/stable/tutorial.html https://www.mongodb.com/languages/python https://zhuanlan.zhihu.com/p/51171906 https://www.runoob.com/python3/python-mongodb.html https://blog.baoshuo.ren/post/luogu-spider/ https://hub.docker.com/_/mongo 安装 MongoDB $ docker search mongo 启动一个mongo服务器实例 $ docker run --name some-mongo -d mongo:tag some-mongo是您要分配给容器的名称,tag是指定您想要的 MongoDB 版本的标签 MongoDB 的默认数据目录路径是/data/db 如下: $ docker run -it -v mongodata:/data/db -p 27017:27017 --name mongodb --restart unless-stopped -d mongo 你应该让 MongoDB 在端口 27017 上运行,并且可以通过localhostWindows 和 Ubuntu 20.04 上的URL访问 http://localhost:27017/ -p 是 HOST_PORT:CLIENT_PORT  -P 随机端口 -p 27017:27017 :将容器的27017 端口映射到主机的27017 端口 -v mongodata:/data/db :将主机中当前目录下的db挂载到容器的/data/db,作为mongo数据存储目录 从另一个 Docker 容器连接到 MongoDB 镜像中的 MongoDB 服务器侦听标准 MongoDB 端口27017,因此通过 Docker 网络连接将与连接到远程mongod. 以下示例启动另一个 MongoDB 容器实例,并mongo针对上述示例中的原始 MongoDB 容器运行命令行客户端,从而允许您针对数据库实例执行 MongoDB 语句: $ docker run -it --network some-network --...

MechanicalSoup

用于自动与网站交互的 Python 库。 MechanicalSoup 自动存储和发送 cookie,跟踪重定向,并且可以跟踪链接和提交表单。 它不执行 JavaScript。 https://github.com/MechanicalSoup/MechanicalSoup https://mechanicalsoup.readthedocs.io/en/stable/index.html https://realpython.com/python-web-scraping-practical-introduction/ pip show Mechanicalsoup 找到模块的安装位置 https://stackoverflow.com/questions/54352162/download-file-with-mechanicalsoup # Install dependencies # pip install requests # pip install BeautifulSoup4 # pip install MechanicalSoup # Import libraries import mechanicalsoup import urllib.request import requests from bs4 import BeautifulSoup import re # Create a browser object that can collect cookies browser = mechanicalsoup.StatefulBrowser() browser.open("https://www.ons.gov.uk/economy/grossdomesticproductgdp/timeseries/l2kq/qna") browser.download_link(link_text=".xls",file="D:/ONS_Data.xls" )

端口映射 公网访问内网

https://portforward.com/ Holer 通过安全隧道将位于NAT和防火墙之后的本地服务器暴露给公共Internet。 Holer是一个将原型中的应用映射到公网访问的端口映射软件,支持转发基于TCP协议的报文 https://github.com/wisdom-projects/holer 方式一:使用(公告)的holer映射或者开通holer服务,通过holer客户端软件经 holer服务器实现公网访问。 公开的holer映射详情如下: 访问密钥 访问域名 公网地址 本地地址 使用场景 HOLER_CLIENT-2F8D8B78B3C2A0AE holer65530.wdom.net holer.org:65530 127.0.0.1:8080 网页 HOLER_CLIENT-3C07CDFD1BF99BF2 holer65531.wdom.net holer.org:65531 127.0.0.1:8088 网页 HOLER_CLIENT-2A623FCB6E2A7D1D holer65532.wdom.net holer.org:65532 127.0.0.1:80 网页 HOLER_CLIENT-AF3E6391525F70E4 不适用 holer.org:65533 127.0.0.1:3389 远程桌面 HOLER_CLIENT-822404317F9D8ADD 不适用 holer.org:65534 127.0.0.1:22 SSH协议 HOLER_CLIENT-27DD1389DF1D4DBC 不适用 holer.org:65535 127.0.0.1:3306 数据库 使用Java版本的holer客户端 ①java 1.7或者更高版本 ②下载holer-client.zip 修改配置文件C:\holer-client\conf\holer.conf HOLER_ACCESS_KEY=HOLER_CLIENT-2A623FCB6E2A7D1D HOLER_SERVER_HOST=holer65532.wdom.net ③建议先双击运行C:\holer-client\bin\shutdown.bat,再双击运行C:\holer-client\bin\startup.bat...