'''
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. (八) .launch文件 ---编写简单的启动脚本文件

    下面我们将介绍,如何编写一个启动脚本程序:(.launch文件) 还记得我们在 创建ROS软件包教程 中创建的第一个程序包(beginner_tutorials)吗,现在我们要使用它. 在 begin ...

  2. Linux移植之配置过程分析

    在Linux移植之移植步骤中已经将Linux移植的过程罗列出来了,现在分析一下Linux的配置过程,将分析以下两个配置过程: 1.make s3c2410_defconfig分析 2.make men ...

  3. sql建立一种,自定义的执行作业

    USE [chongwu] GO /****** Object: StoredProcedure [dbo].[p_createjob] Script Date: 01/21/2016 14:32:0 ...

  4. 去掉easyui datagrid内部虚线的方式。

    去掉easyui        datagrid内部虚线的方式.easyui datagrid的样式是统一写在样式文件中的,如果想要统一替换可以找对应的datagird样式文件中的以下部分.如果想要改 ...

  5. 大数java(pow)

    Problems involving the computation of exact values of very large magnitude and precision are common. ...

  6. CreateToolhelp32Snapshot 以及 EnumProcesses

    RT,请教如何获得Windows任务管理器中所显示的进程列表. CreateToolhelp32Snapshot 以及 EnumProcesses 这些方法我都试过了,但是这两种方法得到的结果和任务管 ...

  7. 配置nginx1.8支持thinkPHP3.2 pathinfo模式

    nginx 下conf/nginx.conf 或者自己的vhosts更改以前的参数 location / { root   html; index  index.html index.htm inde ...

  8. [linux]Linux如何查看文件中的中间部分内容

    最基本的是cat.more和less. 1. 如果你只想看文件的前5行,可以使用head命令,如: head -5 /etc/passwd 2. 如果你想查看文件的后10行,可以使用tail命令,如: ...

  9. sql求倒数第二大的数,效率不高,但写法新颖

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  10. Partition Array Into Three Parts With Equal Sum LT1013

    Given an array A of integers, return true if and only if we can partition the array into three non-e ...