classmethod与staticmethod

1、绑定方法:

  在类内部定义的函数,默认就是给对象来用,而且是绑定给对象用的,称为对象的绑定方法

  绑定对象的方法特殊之处:

  绑定到类的方法特殊之处:

    应该由类来调用,类来调用,会自动将类当作第一个参数传入

import setting
class people:
    def __init__(self, name,age)
        self.name=name
        self.age=age
    def tell(self):
        print('%s:%s'%(self.name,self.age))
    @classmethod
    def from_conf(cls):
        return cls(setting.name,setting.age)
p=people('egon',19)
p.tell()
p1=People(settings.NAME,settings.AGE)
p1.tell()

p2=People(settings.Name,settings.AGE)


p3=People('alex',74)
p3.tell()

print(People.from_conf)
p4=People.from_conf(People)
print(People.from_conf)
p4=People.from_conf()
p4.tell()

2、staticmethod:非绑定方法,就是一个普通函数

特性:即不跟类的绑定,不跟对象绑定,这意味着谁都能用、谁来用都是一个普通函数,也就是

说自动化的值的特性了

import setting
import hash lib
import time
class People:
    def __init__(self, name,age)
        self.uid=self.create_id()
        self.name=name
        self.age=age
    def tell(self):
        print('%s:%s:%s'%(self.uid,self.name,self.age))
    @classmethod #绑定关系    
    def from_conf(cls):
        return cls(setting, name,setting.age)
    @staticmethod #解除绑定关系
    def create_id():
        m=hashlib.md5()
        m.update(str(time.clock).encode('utf-8'))    
        return m.hexdigest()
obj=People('egon',18)
print(obj.uid,obj.name,obj.age)
obj.tell()

print(obj.create_id)#运行的结果是一个函数的内存地址
print(People.create_id)
print(obj.create_id())#运行的结果
print(obj.create_id())

作业:

1、定义MySQL类(参考答案:http://www.cnblogs.com/linhaifeng/articles/7341177.html#_label5)

  1.对象有id、host、port三个属性

  2.定义工具create_id,在实例化时为每个对象随机生成id,保证id唯一

  3.提供两种实例化方式,方式一:用户传入host和port 方式二:从配置文件中读取host和port进行实例化

  4.为对象定制方法,save和get_obj_by_id,save能自动将对象序列化到文件中,文件路径为配置文件中DB_PATH,文件名为id号,保存之前验证对象是否已经存在,若存在则抛出异常,;get_obj_by_id方法用来从文件中反序列化出对象

2、定义一个类:圆形,该类有半径,周长,面积等属性,将半径隐藏起来,将周长与面积开放
参考答案(http://www.cnblogs.com/linhaifeng/articles/7340801.html#_label4)

import hashlib
import time
import setting
import pickle
import os
class MySQl:
    
   def __init__(self,host,port):
       self.id=self.create_id()
       self.host=host
       self.port=port

   def tell(self):
        print('%s:%s'%(self.host,self.port))
   @classmethod
   def from_conf(cls):
       return cls(setting.host,setting.port)

   @staticmethod
   def create_id():
       m=hashlib.md5()
       m.update(str(time.clock()).encode('utf-8'))
       return m.hexdigest()

   def save(self):
       DB_PATH=os.path.dirname(os.path.dirname(__file__))
       path_file=os.path.join(DB_PATH,'id')
       with open (path_file,'wb') as f:
           pickle.dump(self,f)
           f.flush()
           print('存放成功')
   def get_obj_by_id(self):
       DB_PATH = os.path.dirname(os.path.dirname(__file__))
       path_file = os.path.join(DB_PATH, 'id')
       with open(path_file, 'rb') as f:
         return  pickle.load(f)
obj=MySQL('ryan',18)
# print(obj.id,obj.host,obj.port)
# obj.tell()
# print(obj.create_id)
# print(Mysql.create_id)
# print(obj.create_id())
# print(Mysql.create_id())
obj.save()
obj2=obj.get_obj_by_id()
print(obj.id)





import math
class Circle:
    def __init__(self,radius):
        self.radius=radius
    @property
    def area(self):
        return math.pi*self.radius**2
    @property
    def perimeter(self):
        return 2*math.pi*self.radius


c=Circle(10)
print(c.radius)
print(c.area)
print(c.perimeter)
原文地址:https://www.cnblogs.com/wuchenyu/p/8856455.html