zoukankan      html  css  js  c++  java
  • 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())

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

  • 相关阅读:
    hdu 1535 Invitation Cards(spfa)
    hdu 1317 XYZZY
    hdu 1245 Saving James Bond
    hdu 1546 Idiomatic Phrases Game
    hdu 1217 Arbitrage(佛洛依德)
    hdu 1599 find the mincost route(无向图的最小环)
    poj1579
    poj1905
    poj1384
    poj3624
  • 原文地址:https://www.cnblogs.com/onda/p/7008739.html
Copyright © 2011-2022 走看看