Primary study notes for python classes and modules

python class

To declare a class in python, you only need to use the keyword object, the initialization function is __init__(), variables can be initialized, etc.

class MyClass(object):
    def __init__(self):
        return self

Inheritance, you only need to bring the name of the known class in the brackets. It should be noted here that python can have multiple inheritance, that is, you can write n classes in brackets, separated by commas:

class Myclass2(MyClass,MyClass1,...):
	def __init__(self,name):
		self.name = name
		return self
	def method(self):
		return self

Calling: Python calls the attributes of the class very simply. You only need to use .the connection attribute, and you can use the decorator @propertyto convert the method of the class into an attribute call

testClass = MyClass2(name = "abc");
testClass.name可以调用name属性,取值是`abc`

python module

  1. Definition: A module is a file including Python definitions and declarations, where the file name is the module name plus the .py suffix
  2. Import method: import module name, or: from module name import method name 1, method name 2
  3. The concept of a package: a file directory containing __init__.py can be regarded as a package.
  4. You can use the above two ways of importing the package to .separate them. Use the first method and you need to bring all the paths when using it. Use the second method and only need to bring the part behind the import when using it. Reference address: https:/ /docs.python.org/3/tutorial/modules.html#packages

This is the part I learned in the laboratory building tonight. I will share it with you for your reference. I am a beginner in python. If there is anything wrong, please forgive me. Private messages or comments are also welcome. I hope everyone can make progress together.

Guess you like

Origin blog.csdn.net/qinmin1/article/details/88045700