Before you learn the basic grammar python A built-in function in dir. stay python Objects in are almost everywhere , Variables learned before 、 data 、 Functions are objects .
stay python You can use the following two methods to verify whether it is an object :
1、 stay identifier / data ( Or variable name ) Enter one after spot ., Then press one TAB key ,iPython It will prompt the common... That the object can call Method list .
2、 Use built-in functions dir Pass in identifier / data ( Variable name or function name ), You can view all... In the object Properties and methods .
Use... In the function __doc__
demo.__doc
Tips :__ Method name __ ( Two underscores start and end ) The way to format is Python Provided Built-in methods / attribute , Next, we will introduce some common built-in methods / attribute .
Serial number
Method name
type
effect
01
__new__
Method
When you create an object , Will be called automatically
02
__init__
Method
When the object is initialized , Will be called by custom
03
__del__
Method
Before the object is destroyed from memory , Will be called automatically
04
__str__
Method
Returns the description of the object ,print The function output uses
Tips : Make good use of dir() function , In learning, there is no need to memorize a lot of content .
remind : install ipython after , see PyCharm Set up , Make sure Console General settings for Use IPython if available Check the option .
Object orientation is a larger encapsulation , Encapsulate multiple methods in a class , In this way, the objects created by this class can directly call these methods .
stay python To define a class that contains only methods , The syntax is as follows :
class Class name :
def Method 1(self, parameter list ):
pass
def Method 1(self, parameter list ):
pass
The definition format of method is almost the same as that of the previously learned function . difference : The first parameter must be self, You can remember this format first , I'll introduce this later self.
Be careful : The lattice rules of class names should conform to the big hump nomenclature .
When a class definition is complete , If you use this class to create objects , The syntax is as follows :
Object variables = Class name ()
demand : kitten Love eat fish , kitten want drink water
analysis :
1. Define a cat class Cat
2. Define two methods eat and drink
3. According to the demand , There is no need to define attributes
Cat
eat(self)
drink(self)
Sample code :
class Cat:
def eat(self):
print(" Kittens love fish ")
def drink(self):
print(" The kitten needs water ")
# The name of the created cat object is tom
tom = Cat()
# Method of calling object
tom.eat()
tom.drink()
Execution results :
Use... In the main program tom This variable is used to receive a Cat Cat object , And then use tom This variable calls the methods of eating fish and drinking water , As for how to eat fish and drink water , The main program doesn't care .
An obvious feature of object-oriented development , The main program is only responsible for making objects work , Instead of caring about the internal implementation of specific methods , As for the internal implementation of the method, it is encapsulated in Cat In the cat category .
Object oriented is learning Python A difficult part of , It is also a very important knowledge , When I go to the company to do projects in the future, I will inevitably use , I hope Xiaobai, who is learning the opposite, is not afraid , take your time , If you don't understand it over and over again, look for information and understand it , Never say that it is too difficult to give up after learning , How can we learn the following knowledge without gnawing off this bone . stay python Self study network There are dozens of object-oriented video tutorials , There are also written commentary articles . There are also other tutorials such as django Tutorials, etc , Those who are interested can go to the Internet and have a look .