Learn Python in One Minute | Object Oriented (Part 1)

Super invincible dry goods will be pushed to you every day at 18:00! ! !

In object-oriented programming languages, models with the same attributes or capabilities are defined using classes. In the program, you need to write a class that can reflect things in the real world, and create objects based on the class.

Python has been an object-oriented language since its design. Because of this, it is easy to create a class and object in Python.

Introduction to Object-Oriented Technology

Class definition and basic usage

The syntax format of the class is as follows

classClassName:
 Statement

class: keywords defining the class

ClassName: The name of the class. Python stipulates that the first letter of the class must be capitalized.

In a python program, the class can only be used after instantiation. The instantiation of a class is similar to a function call. You can instantiate a class as long as you use the class name and parentheses. A class can be instantiated into multiple instances, and the instances will not affect each other.

The basic process of defining and using classes is as follows:

classMyClass:     #Define MyClass
 " This is a class."
    
myclass =MyClass()     #Instantiate class MyClass
print('Description of output class:')    #Display text information
print(myclass.__doc__)    #Display attribute value
print('Display help information:')help(myclass)

In the above code, a class MyClass is first defined, and there is only one statement in this class, "This is a class", then the class is instantiated, and the attributes of the class are called to display the value of the attribute "doc". Every object in the python language has a __ doc __ attribute, which is used to describe the function of the object.

The result is as follows

Description of output class:
This is a class.
Display help information:
Help on MyClass in module __main__ object:classMyClass(builtins.object)|This is a class.||  Data descriptors defined here:||  __dict__
 |  dictionary for instance variables(if defined)||  __weakref__
 |  list of weak references to the object(if defined)>>>

Class object

In the python language, an object is generated by class instantiation.

The class object supports two operations: attribute reference and instantiation.

Attribute references use the same standard syntax as all attribute references in Python: obj.name.

After the class object is created, all names in the class namespace are valid attribute names. So if the class definition is like this:

classMyClass:"""A simple class instance"""
 i =12345
 def f(self):return'hello world'
 
# Instantiated class
x =MyClass()
 
# Access class properties and methods
print("The attribute i of the MyClass class is:", x.i)print("The output of the method f of the MyClass class is:", x.f())

The above creates a new class instance and assigns the object to the local variable x, which is an empty object.

The output of the above program is:

The attribute i of the MyClass class is: 12345
The output of the method f of the MyClass class is: hello world

The class has a special method called init() (Constructor), which is automatically called when the class is instantiated, like the following:

def __init__(self):
 self.data =[]

The class defines the init () method, and the instantiation operation of the class will automatically call the init () method. Instantiate the class MyClass as follows, the corresponding init() method will be called:

x =MyClass()

Of course, the init () method can have parameters, and the parameters are passed to the instantiation operation of the class through init(). E.g:

classComplex:
 def __init__(self, realpart, imagpart):
  self.r = realpart
  self.i = imagpart
x =Complex(3.0,-4.5)print(x.r, x.i)   #Output result: 3.0-4.5
I am kuls
Welcome to join me on WeChat to exchange and learn to read more exciting articles, you can follow me!
Follow the public account and reply "pdf"
No routine to receive a full set of original Django and flask tutorials ⬇️

Recommended Posts

Learn Python in One Minute | Object Oriented (Part 1)
Learn Python in one minute | Object-oriented (Chinese)
Learn the hard core operation of Python in one minute
10. Object Oriented Python
Concurrent requests in Python (part 2)
Learn about garbage collection in Python
Everything in Python is an object
One picture flow: all built-in exceptions in Python
One article to get regular expressions in Python
Python review one