In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of python object-oriented operation methods, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this python object-oriented operation method article. Let's take a look at it.
Manipulate the Redis cluster:
Import redisclusterimport redis# startup_nodes = [# {"host": "118.24.3.40", "password": "HK139bc&*", "port": 6379}, # {"host": "118.24.3.41", "password": "HK139bc&*", "port": 6379} #] # r = rediscluster.RedisCluster (startup_nodes = startup_nodes Decode_responses=True) # print (r.keys ()) r = redis.Redis (host= "118.24.3.40", password= "HK139bc&*", port=6379,db=4,decode_responses=True) r.set ("ljq:ljq1", "ljj") # folder ljq Key:ljq1print (r.get ("ljq:ljq1")) # take the folder with you when you take the value
Object-oriented:
# class Car:# wheel = instance color = "heise" # name = "feimaqiche" # def fly (self): # print (self.name) # # fmz_car = Car () # instantiate fmz_car: instance object # fmz_car.fly () # call method # self object of this class, who is instantiated and who self represents class Car: wheel = class variable, public Apply def _ init__ (self,color,name): # constructor through self.wheel. When the class is instantiated, the automatically executed function self.color = color self.name = name def _ del__ (self): # destructor The instance executes print ("destructor") def fly (self): name= "www" # local variable print (id (self)) print (self.name) def say (self): print ("% s% s"% (self.name,self.color)) fmz_car = Car ("red") "car") # instantiate fmz_car: instance object fmz_car.fly () # call method fmz_car.say () in the class # call method in class print (id (fmz_car)) fmz_car2 = Car ("yellow" "car") # instantiate fmz_car: instance object fmz_car2.fly () # call the method fmz_car2.say () in the class # call the method in the class # raise actively throws an exception # private method Private variable: it can be called in the _ _ class before, but cannot be called after leaving the class.
Encapsulate the MySQL class:
Import pymysqlfrom day8li.homework.const import mysql_infoclass MySQL: # Classical def _ _ init__ (self,mysql_info Data_type=1): self.mysql_info = mysql_info self.data_type = data_type self.__connect_status = False self.__connect () def _ _ connect (self): print ("start connection mysql") try: self.__conn = pymysql.connect (* * self.mysql_info) except: print ("Database connection error!" ) raise Exception ("database connection error") self.__connect_status = True if self.data_type! = 1: self.__cur = self.__conn.cursor (pymysql.cursors.DictCursor) else: self.__cur = self.__conn.cursor () print ("mysql connection succeeded!") Def execute (self,sql): print ("start sql", sql) try: self.__cur.execute (sql) except: print ("sql incorrect, sql statement is% s"% sql) else: print ("sql execution complete!") Return True def fetchone (self,sql): if self.execute (sql): return self.__cur.fetchone () def fetchall (self Sql): if self.execute (sql): return self.__cur.fetchall () def _ _ del__ (self): self.__close () print ("mysql connection closed") def _ _ close (self): if self.__connect_status: self.__cur.close () self.__conn. Close () if _ _ name__ ='_ main__': my = MySQL (mysql_info) my.execute ("update user_nhy_7 set nick ='du Lala 'where id = 3") ret = my.fetchone ("select * from user_nhy_7 where id = 3") print (ret) ret = my.fetchall ("select * from user_nhy_7") print (ret)
Log printing:
# import logging# log = logging.Logger ("abc" Level= "INFO") # log.info ("") # import loguru# loguru.logger.debug ("aaa") # loguru.logger.info ("aaa") # loguru.logger.warning ("aaa") # loguru.logger.error ("aaa") from loguru import loggerimport syslogger.remove () # clear its default settings # fmt ='{time} | | {level} | | {file.path}: line: {line}: function_name: {function} | msg= {message} 'fmt ='{time} | | msg= {message}'# level file function module time message# logger.add (sys.stdout Level='DEBUG', format=fmt) # when we run locally Print # # enqueue=True asynchronous write log # logger.add ('fmz.log', level='DEBUG', format=fmt, encoding='utf-8',# enqueue=True, rotation='1 day') on the console # how often does rotation generate a log file # retention='10 days') # write in the log file # # logger.info ("3253252") class Log: logger.remove () # clear its default setting fmt ='[{time}] [{level}] [{file.path}: line: {line}: function_name: {function}] | msg= {message}'# level file function module time message logger.add (sys.stdout,level= "DEBUG") Format=fmt) # when we run locally Print logger.add on the console ("test.log", level= "DEBUG", format=fmt,encoding='utf-8',enqueue=True,rotation='1 day',retention='10 days') # write in the log file debug = logger.debug info = logger.info warning = logger.warning error = logger.errorif _ name__ = ='_ main__': Log.info ("xxxx") on "python object-oriented operation methods" this article ends here Thank you for reading! I believe that everyone has a certain understanding of the "python object-oriented operation method" knowledge, if you want to learn more knowledge, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.