Object-Oriented Programming (2) meaning of self, destructor, database class practice

Source: Internet
Author: User
Tags instance method

1. Contact use of the class

Class Baby ():

country = ' China ' #类变量, public variables, each instance can be defined to save memory for common things

def my (self):

self. Name = ' Nuo Hanyang ' #name is a property of this class object .

Def cry (self): # instance method

Print (' Whoa Wah ')

@property

def HHH (self):

Return 198

@classmethod

Def XM (CLS): #cls代表的就是baby

Print (Cls.country)

Print (' I am a class method ')

@staticmethod

Def XH ():

Print (' This is a static method, it's the same as a function not written in the class ')

****

Self represents this class of objects.
Because the variables inside the function are local variables, the function cannot be used.
After binding the object with self, you can self.xx it.
****

#1, do not instantiate, call this XM method directly with the class name

After #2, instantiation, call the XM method after instantiating the object, and see that you can pass the memory address to the class without instantiating it, so that the values of different instances are obtained by different memory addresses (self means the memory address itself)

#你不想实例化的时候, you can define a class method

#静态方法就是一个普通函数, it's just written in a class, it doesn't use class variables, class methods, instance variables, instance methods

# BABY.XH ()

# baby.country= ' USA ' #类变量是可以直接通过类名. XXX to be modified.

#

# DCG = Baby ()

# # dcg.country = ' Japan '

# Print (dcg.country)

# YJT = Baby ()

# Print (yjt.country)

# b = Baby ()

# Print (B.HHH)

# DCG = Baby (' Spring ')

# print (' Dong Chunguang memory ', ID (DCG)) **self memory address is the same as changing the memory address.

# zll = Baby (' tensor flow ')

# print (' Dong Chunguang memory ', ID (ZLL)) **self memory address is the same as changing memory address

2. Using classes to overwrite database connections

Import Pymysql

Class MyDb (object):

def __del__ (self):

Executed at the end of the #析构函数

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, #自动提交

)

Except Exception as E:

Print (' Database connection failed! %s '%e)

Else

self. Cur = self. Coon.cursor (cursor=pymysql.cursors.dictcursoR) #这样返回的就是字典

def ex_sql (Self,sql):

Try

Self.cur.execute (SQL)

Except Exception as E:

Print (' SQL statement problematic,%s '%sql)

Else

Self.res = Self.cur. Fetchall ()

Return Self.res

###

Destructors
Def __del__ ():
Pass
Executed when the instance is destroyed.
######

my = MyDb (' 118.24.3.40 ', ' jxz ', ' 123456 ', ' jxz ')

My.ex_sql (' select * from Stu; ')

Print (My.res)

Print (' I'm the last line of code ...  ') #这里才会执行析构函数. The destructor is executed when the instance is destroyed .

Object-Oriented Programming (2) meaning of self, destructor, database class practice

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.