通常情况下,类中函数中定义的所有函数,,都是对象的绑定方法,除此之外,还有专门的静态方法和类方法,这两个是专门给使用的,但是对象非要调用也是不会报错的。

对象在调用的时候会把自己传递给self,也就是绑定方法的第一个参数。

1 静态方法

这里定义spam的时候没有self,而是传入了xyz,类在使用的时候必须传入3个参数

class Foo:
@staticmethod # spam = staticmethod(apam)
def spam(x,y,z):
print(x,y,z)
# spm = staticmethod(spam) Foo.spam(1,2,3)

应用场景

首相先了解下时间模块的使用

>>>import time
>>> time.localtime()
time.struct_time(tm_year=2017, tm_mon=4, tm_mday=22, tm_hour=20, tm_min=7, tm_se
c=41, tm_wday=5, tm_yday=112, tm_isdst=0)
>>> t=time.localtime()
>>> print(t.tm_year)
2017

应用场景

# 应用场景
import time
class Date:
def __init__(self,year,month,day):
self.year = year
self.month = month
self.day = day @staticmethod
def now():
t = time.localtime()
obj = Date(t.tm_year,t.tm_mon,t.tm_mday)
return obj # d1 = Date(2017,1,12)
# print(d1.year)
date_now =Date.now() # 使用的时候就不同传参数了 直接调用
print(date_now.year)
print(date_now.month)
print(date_now.day)

**date_now =Date.now() **对类来说就是一种实例化,

专门给类用的

增加一个新的

import time
class Date:
def __init__(self,year,month,day):
self.year = year
self.month = month
self.day = day @staticmethod
def now():
t = time.localtime()
obj = Date(t.tm_year,t.tm_mon,t.tm_mday)
return obj
@staticmethod
def tomorrow():
t1 = time.localtime(time.time()+86400)
obj = Date(t1.tm_year, t1.tm_mon, t1.tm_mday)
return obj
# d1 = Date(2017,1,12)
# print(d1.year)
date_now =Date.now() # 使用的时候就不同传参数了 直接调用
print(date_now.year)
print(date_now.month)
print(date_now.day)
date_tomorrow = Date.tomorrow() #此时使用的是调用方法是一样的
print(date_tomorrow.year)
print(date_tomorrow.month)
print(date_tomorrow.day)

2 类方法

类方法是专门类的绑定方法

class Foo:
def bar(self):
pass
@classmethod # 类的绑定方法
def test(cls,x): # 可以传入多个值
print(cls,x)
print(Foo.bar)
print(Foo.test)

结果:

function Foo.bar at 0x0000000002B0AB70> 类的函数

bound method Foo.test of <class 'main.Foo'>> 类的绑定方法

类的绑定方法和对象的绑定方法是一样的,会把类本身当做第一个参数传递给类的方法

class Foo:
def bar(self):
pass
@classmethod
def test(cls,x):
print(cls,x) #cls是类的内存地址
cls() # 类实例化
# print(Foo.bar)
# print(Foo.test) Foo.test(124)
f = Foo() #实例化一个对象
print(f.test)
print(Foo.test)

结果:

bound method Foo.test of <class 'main.Foo'>>

bound method Foo.test of <class 'main.Foo'>>

打印的的都是类的绑定方法,

即便是实例一个对象,f.test(123) 也是使用的类的绑定方法

获得了类的内存地址,加括号就能实例化

str

定义在类的内部,必须返回一个字符串类型

什么时候出发执行?打印这个类的对象时,会出发执行

class People:

def init(self,name,age):

self.name=name

self.age = age

def __str__(self):
return 'name:%s,age:%s' %(self.name,self.age)

p1=People("aa",18)

print(p1)

应用场景

import time
class Date:
def __init__(self,year,month,day):
self.year = year
self.month = month
self.day = day @staticmethod
def now():
t = time.localtime()
obj = Date(t.tm_year,t.tm_mon,t.tm_mday)
return obj
@staticmethod
def tomorrow():
t1 = time.localtime(time.time()+86400)
obj = Date(t1.tm_year, t1.tm_mon, t1.tm_mday)
return obj
class EuropeDate(Date): # 定义一个子类类继承
pass e1 = EuropeDate.now() # 实例化
print(e1) # 打印这个实例化的对象的时候仍然是Date的对象

结果:

main.Date object at 0x0000000002330B38>

也就是说自己儿子确实别人的

解决方案



import time
class Date:
def __init__(self,year,month,day):
self.year = year
self.month = month
self.day = day @classmethod
def now(cls):
print(cls) # 打印一下,测试传入的类是
t = time.localtime()
obj = Date(t.tm_year,t.tm_mon,t.tm_mday)
return obj
@classmethod
def tomorrow(cls):
t1 = time.localtime(time.time()+86400)
obj = Date(t1.tm_year, t1.tm_mon, t1.tm_mday)
return obj
class EuropeDate(Date): # 定义一个子类类继承
def __str__(self):
return 'year:%s,month:%s,day:%s' %(self.year,self.month,self.day) e1 = EuropeDate.now() #

通过测试,此时打印的的类是Europe

class 'main.EuropeDate'>

终极版:

import time
class Date:
def __init__(self,year,month,day):
self.year = year
self.month = month
self.day = day @classmethod
def now(cls):
print(cls) # 打印一下,测试传入的类是
t = time.localtime()
obj = cls(t.tm_year,t.tm_mon,t.tm_mday)
return obj
@classmethod
def tomorrow(cls):
t1 = time.localtime(time.time()+86400)
obj = cls(t1.tm_year, t1.tm_mon, t1.tm_mday)
return obj
class EuropeDate(Date): # 定义一个子类类继承
def __str__(self):
return 'year:%s,month:%s,day:%s' %(self.year,self.month,self.day) e1 = EuropeDate.now() #
print(e1)

结果是:

class 'main.EuropeDate'>

year:2017,month:4,day:23

在程序中改的是

**obj = cls(t.tm_year,t.tm_mon,t.tm_mday) **,这是用子类实例化

引入cls就是谁来调用,谁就执行

Python静态方法 类方法的更多相关文章

  1. python静态方法类方法属性方法

    Python的静态方法和类成员方法都可以被类或实例访问,两者概念不容易理清,但还是有区别的: 1)静态方法无需传入self参数,类成员方法需传入代表本类的cls参数: 2)从第1条,静态方法是无法访问 ...

  2. python 静态方法、类方法(二)

    <Python静态方法.类方法>一文中曾用在类之外生成函数的方式,来计算类的实例的个数.本文将探讨用静态方法和类方法来实现此功能. 一使用静态方法统计实例 例1.static.py # - ...

  3. 关于python的类方法、实例方法和静态方法区别

    python的类方法需要在方法前面加装饰器:@classmethod ,静态方法是在方法前面加装饰器:@staticmethod. 类方法.类属性是属于类自身,属于类自身的命名空间,和实例方法.实例属 ...

  4. Python静态方法(staticmethod)和类方法(classmthod)

    Python静态方法(staticmethod)和类方法(classmthod)翻了翻之前的笔记,也刚好看到一篇不错的blog,关于静态方法和类方法的,方便以后查阅,就写在这里了,废话不多说,直接上代 ...

  5. Python 静态方法、类方法和属性方法

    Python 静态方法.类方法和属性方法 静态方法(staticmethod) staticmethod不与类或者对象绑定,类和实例对象都可以调用,没有自动传值效果,Python内置函数staticm ...

  6. IOS—静态方法(类方法)和实例方法

    1.实例方法/动态方法 a).标识符:- b).调用方式:(实例对象    函数) c).实例方法在堆栈上. 2.静态方法/类方法 a).标识符:+ b).调用方式:(类    函数) c).静态方法 ...

  7. IOS基础——静态方法(类方法)和实例方法

    1.实例方法/动态方法 a).标识符:- b).调用方式:(实例对象    函数) c).实例方法在堆栈上. 2.静态方法/类方法 a).标识符:+ b).调用方式:(类    函数) c).静态方法 ...

  8. Java学习日记基础(五)——类、对象之this、静态变量(类变量)、静态方法(类方法)、四大特征

    this 赵本山问奥尼尔:“我的爸爸的爸爸是谁?” 奥尼尔:“不知道” 赵本山:“你傻啊,是我爷爷” 奥尼尔回去问科比:“我的爸爸的爸爸是谁?” 科比:“不知道” 奥尼尔:”你傻啊,是赵本山的爷爷“ ...

  9. python类的实例方法\静态方法\类方法区别解析(附代码)

    前言 搞了好久python,一直搞得不太清楚这几种类的方法,今天花时间好好测试一下,算是弄懂点皮毛吧. 三种方法的不同 先剽窃个图看一下 可以看到,实例是三种方法都可以调用的,而类只可以调用两种.所以 ...

随机推荐

  1. Python包管理工具小结

    此文已由作者张耕源授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 作为一名接触Python有一段时间的初学者,越来越体会到Python的方便之处,它使人能更 多的关注业务本身 ...

  2. Vue.js中,如何自己维护路由跳转记录?

    在Vue的项目中,如果我们想要做返回.回退操作时,一般会调用router.go(n)这个api,但是实际操作中,使用这个api有风险,就是会让用户跳出当前应用,因为它记录的是浏览器的访问记录,而不是你 ...

  3. 无监督学习:Deep Generative Mode(深度生成模型)

    一 前言 1.1 Creation 据说在费曼死后,人们在他生前的黑板上拍到如图画片,在左上角有道:What i cannot create ,I do not understand. Generat ...

  4. SCUT - 336 - 酋雷姆 - 最小生成树

    每个世界可以和别的世界连通,也可以直接联通虚拟的已经毁灭的世界,这样变成一个最小生成树问题. 但是好像哪里不对? 有人用dp过掉的? 不太清楚怎么搞的. 其实就是最小生成树-- #include< ...

  5. python 之 日志输出格式

    # 定义日志文件的路径LOG_PATH=r'D:\code\SH_fullstack_s1\day15\ATM\log\access.log'BOSS_LOG_PATH=r'D:\code\SH_fu ...

  6. ThinkSNS+ 是如何计算字符显示长度的

    什么是ThinkSNS+ ThinkSNS(简称TS),一款全平台综合性社交系统,目前最新版本为ThinkSNS+.ThinkSNS V4 ThinkSNS[简]. 今天我们来聊一下可能很多人都会头疼 ...

  7. C#代码生成器附百度云盘源码地址

    今晚闲着没事,写了个代码生成器,在这里只做个抛砖引玉,后面可以继续扩展功能,下方附百度云盘源码地址. 使用数据库:sqlserver 编译器:vs2015 废话不多说,上界面: 程序主界面: 数据库: ...

  8. 【T-BABY 夜谈大数据】基于内容的推荐算法

    这个系列主要也是自己最近在研究大数据方向,所以边研究.开发也边整理相关的资料.网上的资料经常是碎片式的,如果要完整的看完可能需要同时看好几篇文章,所以我希望有兴趣的人能够更轻松和快速地学习相关的知识. ...

  9. Domination

    题目链接 #include <bits/stdc++.h> using namespace std; typedef long long ll; inline ll read(){ , f ...

  10. day2逻辑运算作业详解

    1.day2题目 1.判断下列逻辑语句的True,False. 1)1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 &l ...