Skip to main content

classmethod

import setting

class Mysql:

	def __init__(self,ip,port):
		self.ip = ip
		self.port = port

	def func(self):
		print('%s %s' %(self.ip,self.port))

	@classmethod  ## 提供一种初始化对象的方法
	def from_conf(cls):
		return cls(setting.IP,setting.PORT)  ## 返回的就是一个实例化的对象,不需要我自己一个个创建

obj = Mysql.from_conf()
print(obj.__dict__)  ## {'ip': '127.0.0.1', 'port': 3306}