1. class method Connection Database
Let's talk about the destructor.
def __del__ (self): Pass
destructors, which are executed when the instance is destroyed, then for the connection to the database, after the instance executes, the cursor and database connections need to be closed, both of which can be put into a destructor
ImportPymysqlclassMydb (object):def __del__(self):#destructor that executes when the instance is destroyed.self.cur.close () self.coon.close ( )Print(' Over') def __init__(self,host,user,passwd,db, Port=3306,charset='UTF8'): Try: Self.coon=Pymysql.connect (Host=host,user=user,passwd=passwd,port=Port, CharSet=charset,db=db,autocommit=True)#autocommit automatically Commit, write the statement when the automatic submission exceptException as E:Print('Database connection Failed! %s'%e)Else: Self.cur=self.coon.cursor (cursor=pymysql.cursors.DictCursor)defExsql (self,sql):Try: Self.cur.execute (SQL)exceptException as E:Print('There is a problem with the SQL statement,%s'%sql)Else: Self.res=Self.cur.fetchall ()returnSelf.resmy=mydb (' xxx','Jxz','123456','Jxz') My.exsql ('select * from Stu;')Print('I am the last line of code')
In the constructor, make the database connection, do the exception processing, execute the SQL statement in the executing SQL statement function, the operation of the destructor closes the cursor and the database connection.
In connection with the database, there is a new knowledge point: autocommit=true auto-commit, which was not mentioned in the previous database connection use, instead of the need to determine whether the SELECT statement, whether the need to commit.
2.If __name__== ' __main__ ' usage
If __name__== ' __main__ ':
Usually used in testing or commissioning.
This is useless if you run the Python file directly.
Other python files are normally imported into the Python file, and the Python file is executed normally
However, if the imported Python file has the If __name__== ' __main__ ': This execution result, then the imported Python file will not be executed
3. Class variables, instance variables and instance methods
1) Concept
Class variables: Common variables, each instance can be used
Instance variable: member variable
Instance method: Can be used after instantiation
2) Self
Self represents the object, because the variables inside the function are local variables, out of the function can not be used, with self to the object after binding, you can self.xx casually use
Self is the same as the memory address of the instance object
class Baby (): def __init__ (self,name): Print (' self's memory address ', id (self)) DCG=baby (' test ' ) Print(' tested memory address ', ID (DCG))
The output is:
Self's memory address 801098625712801098625712
3) class variables, instance variables, and instance methods used
classBaby (): Country=' China' #class variables, common variables, each instance can be used defMy (self): Self.name='Liujia'ABC=Baby ()Print(Abc.country)#using the value of a class variableBaby.country='USA' #class variables can be modified directly from the class name. XXXyjt=Baby ()Print(Yjt.country)#class variable has been modified to USADCG=Baby () dcg.country='Japan' #instance VariablePrint(Dcg.country)
Look at the above code, the country variable is under the class, but not in other functions, is a class variable, each instance can be used;
The My () function is an instance method that can be used after instantiation;
First look at the example of ABC, print country, then use a class variable, the output is China, and then the class variable is modified, this time to instantiate YJT, the output of country is the USA, the final instance of the
DCG, this instance modifies the value of country, and the final output of country is Japan.
4) concept of adorner 4, class method, static Method 1)
Class method:
You can call it directly without instantiating it.
Class variables can be used through the CLS
Cannot invoke instance method
When you do not want to instantiate, you can define a class method
static method:
A normal function, just written in a class, not using class variables, class methods, instance variables, and instance methods
2) class method
class Baby (): ='China' # class variables, common variables, each instance can be used @classmethod def XM (CLS):#CLS stands for Baby, class method print(cls.country) Print (' I am a class method ')
Class methods can be called directly without instantiation.
BABY.XM () # does not instantiate, the direct class name calls XM this method
The output is:
China I am the class method
Of course, you can instantiate it first, and then call
dcg=Baby () dcg.xm () # after instantiation, call the XM method after instantiating the object
The output is the same.
Also, there is no way to invoke instance variables and instance methods inside a class method, such as
classBaby (): Country=' China' #class variables, common variables, each instance can be used defMy (self): Self.name='Liujia' defCry (self):Print('HHH') @classmethoddefXM (CLS):#The CLS represents the baby, the class method Print(cls.country)Print('I am the class method') Cls.name#cannot call instance variableCls.cry ()#cannot invoke instance methodbaby.xm ()
Whether it is Cls.name or cls.cry (), but will run the error
3) static method
class Baby (): @staticmethod # static method def xh (): Print (' This is a static method, it's the same as a function not written in the class ')
It can be noted that the function inside this class does not have self, which is a normal function, but it still needs to be instantiated before it can be called.
And invoking instance variables, instance methods, and so on are not possible.
classBaby (): Country=' China' #class variables, common variables, each instance can be used defMy (self): Self.name='Liujia' defCry (self):Print('HHH') @staticmethod#Static Methods defxh ():Print('This is a static method, which is the same as a function that is not written in the class.') Self.name self.cry ()
Self.name and Self.cry () but it will be an error.
Python Learning (29) class method connection database and object-oriented programming (2)