python_day7 绑定方法与非绑定方法

在类中定义函数如果 不加装饰器 则默认 为对象作为绑定方法

如果增加 classmethod 是 以 类 作为绑定方法

增加 classmethod 是 非绑定方法,就是不将函数 绑定

#####################

class Foo:
def func(self):
print(self)
@classmethod
def func2(cls):
print(cls)
@staticmethod
def sta():
print('非绑定参数')

JG=Foo()
JG.func()
JG.func2()
JG.sta()

########################

绑定方法与 非绑定方法的

应用场景;Mysql 连接; 

绑定对象方法:默认传值

绑定类方法: 可以从文件中读取 默认值

非绑定方法,通过time.clock() 生成ID

#############################

 

import set1
import hashlib
import time
class Mysql:
def __init__(self,host,port):
self.id=self.create_id()
self.host=host
self.port=port
@classmethod
def from_conf(cls):
return cls(set1.Host,set1.Port)
@staticmethod
def create_id():
HAS=hashlib.md5(str(time.clock()).encode('utf-8'))
return HAS.hexdigest()
@classmethod
def dele(cls):
print('from,delet')

def select(self):
print('select')

JG=Mysql('192.168.1.1','3306')
JG.select()
print(JG.create_id())

#############################################

 

posted @ 2017-06-14 14:37  昂达达  阅读(186)  评论(0编辑  收藏  举报