'''
1 绑定方法:
在类内部定义的函数,默认就是给对象来用,而且是绑定给对象用的,称为对象的绑定方法
绑定对象的方法特殊之处:
应该由对象来调用,对象来调用,会自动将对象当作第一个参数传入

绑定到类的方法特殊之处:
应该由类来调用,类来调用,会自动将类当作第一个参数传入

'''
import settings

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(settings.NAME,settings.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 settings
# import hashlib
# 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(settings.NAME,settings.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(People.create_id())

classmethod,staticmethod的更多相关文章

  1. @classmethod @staticmethod 个人理解

    官方解释 @classmethod 一个类方法把类自己作为第一个实参, 就像一个实例方法把实例自己作为第一个实参. 语法格式: class C: @classmethod def f(cls, arg ...

  2. 初识面向对象(钻石继承,super,多态,封装,method,property,classmethod,staticmethod)

    组合 什么有什么的关系 一个类的对象作为另一个类的对象继承 子类可以使用父类中的名字(静态属性 方法)抽象类和接口类 只能不继承,不能被实例化 子类必须实现父类中的同名方法———规范代码 metacl ...

  3. python面试题之下面这些是什么意思:@classmethod, @staticmethod, @property?

    回答背景知识 这些都是装饰器(decorator).装饰器是一种特殊的函数,要么接受函数作为输入参数,并返回一个函数,要么接受一个类作为输入参数,并返回一个类. @标记是语法糖(syntactic s ...

  4. Python类中装饰器classmethod,staticmethod,property,

    @classmethod 有的时候在类中会有一种情况,就是这个方法并不需要使用每一个对象属性 因此 这个方法中的self参数一个完全无用的参数,使用classmethod class A: __cou ...

  5. 【python】classmethod & staticmethod 区别

    来源:http://blog.csdn.net/carolzhang8406/article/details/6856817 其他参考: http://blog.csdn.net/lovingprin ...

  6. 记录一个 关于 python 普通方法,静态方法和类方法 的介绍。@classmethod @staticmethod

    上班时间 只贴看到最厉害的答案 回头总结 http://stackoverflow.com/questions/12179271/python-classmethod-and-staticmethod ...

  7. python中@classmethod @staticmethod区别

    Python中3种方式定义类方法, 常规方式, @classmethod修饰方式, @staticmethod修饰方式. class A(object): def foo(self, x): prin ...

  8. python笔记---@classmethod @staticmethod

    python定义类方法的三种方式: 1.常规方式--需要通过self参数隐式的传递当前类对象的实例 2.@classmethod修饰方式--@classmethod修饰的方法class_foo()需要 ...

  9. python中@classmethod @staticmethod区别(转)

    pthon中3种方式定义类方法, 常规方式, @classmethod修饰方式, @staticmethod修饰方式. class A(object): def foo(self, x): print ...

  10. python 全栈开发,Day22(封装,property,classmethod,staticmethod)

    一.封装 封装 : 广义上的 :把一堆东西装在一个容器里 狭义上的 :会对一种现象起一个专门属于它的名字 函数和属性装到了一个非全局的命名空间 —— 封装 隐藏对象的属性和实现细节,仅对外提供公共访问 ...

随机推荐

  1. Json解析数据

    Json数据解析(重点网址推荐:www.json.org   code.google.com/   https://www.json.com/) 1:什么是Json? 2:Json数据格式的特点? 3 ...

  2. poj_1979(dfs)

    Red and Black There is a rectangular room, covered with square tiles. Each tile is colored either re ...

  3. Oracle Linux下数据库操作的相关问题

    1.su - oracle 切换到oracle用户 lsnrctl status 查看数据库监听状态 lsnrctl start 打开数据库监听 2.Connected to an idle inst ...

  4. CODE[VS]2494 Vani和Cl2捉迷藏

    原题链接 这里有一个结论:最多能选取的藏身点个数等于最小路径可重复点覆盖的路径总数. 所以我们可以先传递闭包,然后求最小路径点覆盖即可. #include<cstdio> #include ...

  5. 无法创建.gitignore文件,提示必须输入文件名称

    If you're using Windows it will not let you create a file without a filename in Windows Explorer. It ...

  6. PropertyPlaceholderConfigurer

    PropertyPlaceholderConfigurer Spirng在生命周期里关于Bean的处理大概可以分为下面几步: 加载 Bean 定义(从xml或者从@Import等) 处理 BeanFa ...

  7. 企业官网Web原型制作分享-Tesla

    Tesla是汽车行业知名的奢华品牌,产品为纯电动汽车,知名度极高.此模板正是取自Tesla的官网,高端大图配上文字排版,彰显了汽车的奢华感觉. 本原型由国产Mockplus(原型工具)和iDoc(智能 ...

  8. Spark Streaming性能调优详解

    Spark Streaming性能调优详解 Spark  2015-04-28 7:43:05  7896℃  0评论 分享到微博   下载为PDF 2014 Spark亚太峰会会议资料下载.< ...

  9. Controller异步模式

    转载: https://blog.csdn.net/yingxiake/article/details/51193319 因为服务器请求处理线程的总数是有限的,如果类似的请求多了,所有的处理线程处于阻 ...

  10. java15

    1.数组 格式:数据类型 [ ] 数据名称 = new 数据类型 [ ] { }: 2.初始化 静态初始化(已知要开多少个房间来存储数据) int[ ] a =new int[ ] {12,32,54 ...