classmethod,staticmethod
'''
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的更多相关文章
- @classmethod @staticmethod 个人理解
官方解释 @classmethod 一个类方法把类自己作为第一个实参, 就像一个实例方法把实例自己作为第一个实参. 语法格式: class C: @classmethod def f(cls, arg ...
- 初识面向对象(钻石继承,super,多态,封装,method,property,classmethod,staticmethod)
组合 什么有什么的关系 一个类的对象作为另一个类的对象继承 子类可以使用父类中的名字(静态属性 方法)抽象类和接口类 只能不继承,不能被实例化 子类必须实现父类中的同名方法———规范代码 metacl ...
- python面试题之下面这些是什么意思:@classmethod, @staticmethod, @property?
回答背景知识 这些都是装饰器(decorator).装饰器是一种特殊的函数,要么接受函数作为输入参数,并返回一个函数,要么接受一个类作为输入参数,并返回一个类. @标记是语法糖(syntactic s ...
- Python类中装饰器classmethod,staticmethod,property,
@classmethod 有的时候在类中会有一种情况,就是这个方法并不需要使用每一个对象属性 因此 这个方法中的self参数一个完全无用的参数,使用classmethod class A: __cou ...
- 【python】classmethod & staticmethod 区别
来源:http://blog.csdn.net/carolzhang8406/article/details/6856817 其他参考: http://blog.csdn.net/lovingprin ...
- 记录一个 关于 python 普通方法,静态方法和类方法 的介绍。@classmethod @staticmethod
上班时间 只贴看到最厉害的答案 回头总结 http://stackoverflow.com/questions/12179271/python-classmethod-and-staticmethod ...
- python中@classmethod @staticmethod区别
Python中3种方式定义类方法, 常规方式, @classmethod修饰方式, @staticmethod修饰方式. class A(object): def foo(self, x): prin ...
- python笔记---@classmethod @staticmethod
python定义类方法的三种方式: 1.常规方式--需要通过self参数隐式的传递当前类对象的实例 2.@classmethod修饰方式--@classmethod修饰的方法class_foo()需要 ...
- python中@classmethod @staticmethod区别(转)
pthon中3种方式定义类方法, 常规方式, @classmethod修饰方式, @staticmethod修饰方式. class A(object): def foo(self, x): print ...
- python 全栈开发,Day22(封装,property,classmethod,staticmethod)
一.封装 封装 : 广义上的 :把一堆东西装在一个容器里 狭义上的 :会对一种现象起一个专门属于它的名字 函数和属性装到了一个非全局的命名空间 —— 封装 隐藏对象的属性和实现细节,仅对外提供公共访问 ...
随机推荐
- 点线特征双目视觉SLAM---暑期笔记
1.由于以后可能研究有关基于特征方面的SLAM研究,所以近期看了一篇文章[基于点线综合特征的双目视觉SLAM方法--谢晓佳],由于之前对SLAM的模块比较模糊,所以认真阅读了此论文,并对主要的3个线程 ...
- 关于transform-style:preserve-3d的些许明了
父元素要添加属性transform-style:preserve-3d;和transform:perspective(800px);还有相对定位 首先设置子元素 具有3D属性,然后再设置视角与3D元素 ...
- c#特性attribute:
特性是被编译到metadata中, 是提供给反射用的. 特性attribute:1 什么是attribute,和注释有什么区别 2 声明和使用attribute3 使用attribute完成扩展4 ...
- iOS.Debug.Simulator
1. iOS Simulator Tips & Tricks http://code.tutsplus.com/tutorials/ios-simulator-tips-tricks--mob ...
- RTTI(运行时类型识别)
运行时类型识别(Run-time type identification , RTTI),是指在只有一个指向基类的指针或引用时,确定所指对象的准确类型的操作.其常被说成是C++的四大扩展之一(其他三个 ...
- laravel目录结构
- samtools
samtools 用法 samtools <command> [options] command 见以下列表, 每个 command 的 options 也不同 dict faidx in ...
- hbase 无法打开60010网页
在hbase-site.xml中添加如下节点 <property> <name>hbase.master.info.port</name> <value> ...
- 学习selenium的过程
- [转载]How To Install Nginx And PHP-FPM On CentOS 6 Via Yum
http://www.lifelinux.com/how-to-install-nginx-and-php-fpm-on-centos-6-via-yum/ http://blog.csdn.net/ ...