1.classmethod

 

@classmethod   # 把一个对象绑定的方法 修改成一个 类方法
第一,在方法中仍然可以引用类中的静态变量
第二,可以不用实例化对象,就直接用类名在外部调用这个方法
什么时候用@classmethod?
1.定义了一个方法,默认传self,但这个self没被使用
2.并且你在这个方法里用到了当前的类名,或者你准备使用这个类的内存空间中的名字的时候

import time
class Date:
def __init__(self,year,month,day):
self.year = year
self.month = month
self.day = day
@classmethod
def today(cls):
struct_t = time.localtime()
date = cls(struct_t.tm_year,struct_t.tm_mon,struct_t.tm_mday)
return date date对象 = Date.today()
print(date对象.year)
print(date对象.month)
print(date对象.day)

2. @staticmethod 被装饰的方法会成为一个静态方法

class User:
pass
@staticmethod
def login(a,b): # 本身是一个普通的函数,被挪到类的内部执行,那么直接给这个函数添加@staticmethod装饰器就可以了
print('登录的逻辑',a,b)
# 在函数的内部既不会用到self变量,也不会用到cls类 obj = User()
User.login(1,2)
obj.login(3,4)
class A:
country = '中国'
def func(self):
print(self.__dict__)
@classmethod
def clas_func(cls):
print(cls)
@staticmethod
def stat_func():
print('普通函数')
@property
def name(self):
return 'wahaha'

小结:

  能定义到类中的内容

  • 静态变量 是个所有的对象共享的变量 有对象\类调用 但是不能重新赋值
  • 绑定方法 是个自带self参数的函数 由对象调用
  • 类方法 是个自带cls参数的函数 由对象\类调用
  • 静态方法 是个啥都不带的普通函数 由对象\类调用
  • property属性 是个伪装成属性的方法 由对象调用 但不加括号

@staticmethod 不需要访问和类相关的属性或数据

@classmethod 可以访问和类相关(不和实例相关)的属性

如果你定义了一个方法它的返回值永远和类的属性及实例无关,结果永远不变,就用@staticmethod

如果你定义了一个方法它的返回值和只和类的属性有关,结果可变.就用@classmethod

classmethod与staticmethod的更多相关文章

  1. python基础知识讲解——@classmethod和@staticmethod的作用

    python基础知识讲解——@classmethod和@staticmethod的作用 在类的成员函数中,可以添加@classmethod和@staticmethod修饰符,这两者有一定的差异,简单来 ...

  2. Python中的classmethod与staticmethod

    首先,这是一个经典的问题. 我们首先做一个比较: classmethod的第一个参数是cls,即调用的时候要把类传入 这意味着我们我们可以在classmethod里使用类的属性,而不是类的实例的属性( ...

  3. classmethod 和 staticmethod

    我一般很少用到. Talk is cheap, show you the code. #!/usr/bin/env python # -*- coding: utf-8 -*- ########### ...

  4. 洗礼灵魂,修炼python(47)--巩固篇—定义类的方法之@classmethod,@staticmethod

    定义类的方法,相信你会说,不就是在class语句下使用def () 就是定义类的方法了嘛,是的,这是定义的方法的一种,而且是最普通的方式 首先,我们已经知道有两种方式: 1.普通方法: 1)与类无关的 ...

  5. python基础-abstractmethod、__属性、property、setter、deleter、classmethod、staticmethod

    python基础-abstractmethod.__属性.property.setter.deleter.classmethod.staticmethod

  6. 【python】Python 中的 classmethod 和 staticmethod

    Python 中的 classmethod 和 staticmethod 有什么具体用途? 推荐地址:http://www.cnblogs.com/wangyongsong/p/6750454.htm ...

  7. python classmethod 和 staticmethod的区别

    https://stackoverflow.com/questions/12179271/meaning-of-classmethod-and-staticmethod-for-beginner 1. ...

  8. python的@classmethod和@staticmethod

    本文是对StackOverflow上的一篇高赞回答的不完全翻译,原文链接:meaning-of-classmethod-and-staticmethod-for-beginner Python面向对象 ...

  9. python 封装,隐藏属性,绑定方法classmethod和staticmethod

    [封装] 隐藏对象的属性和实现细节,仅对外提供公共访问方式. [好处] 1. 将变化隔离: 2. 便于使用: 3. 提高复用性: 4. 提高安全性: [封装原则] 1. 将不需要对外提供的内容都隐藏起 ...

  10. Fluent Python: Classmethod vs Staticmethod

    Fluent Python一书9.4节比较了 Classmethod 和 Staticmethod 两个装饰器的区别: 给出的结论是一个非常有用(Classmethod), 一个不太有用(Static ...

随机推荐

  1. redis启动报错:The Windows version of Redis allocates a memory mapped heap for sharing with

    windows系统下通过cmd命令:redis-server.exe redis.windows.conf 启动redis报错,控制台报错如下: The Windows version of Redi ...

  2. PHP min() 函数

    实例 通过 min() 函数查找最小值: <?php高佣联盟 www.cgewang.comecho(min(2,4,6,8,10) . "<br>");echo ...

  3. springboot多数据源启动报错:required a single bean, but 6 were found:

    技术群: 816227112 参考:https://stackoverflow.com/questions/43455869/could-not-autowire-there-is-more-than ...

  4. Jenkins总结3-shell脚本

    我写shell脚本的功力还很初级,基本都是现学现卖,写得不是很健壮,只能提供个思路,请大家包涵. 我使用的系统只能发函数放到shell最前面.本人还是比较推崇函数式脚本的,方便复用,目前只简单的封装了 ...

  5. TF上架模式是什么?有什么作用?

    TF上架模式中的TF上架就是TestFlight上架的意思,意思就是将开发者开发完成的App在苹果官方内测商店TestFlight上架的模式,一般被我们简称为TF上架模式. 为什么要了解TF上架呢?为 ...

  6. (转)交叉编译lrzsz

    交叉编译lrzsz 2016-03-20 1. 系统环境: Distributor ID:    Ubuntu Description:    Ubuntu 14.04.1 LTS Release:  ...

  7. 15、Facade 外观模式

    什么是Facade模式 随着系统越来越复杂,我们需要把细节隐藏起来,给客户端提供一个统一的接口.在这种需求下facade模式诞生了.该模式比较简单,我们只需要在系统变得复杂把它运用上来,这样底层跟客户 ...

  8. one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [3, 1280, 28, 28]], which is output 0 of LeakyReluBackward1, is at version 2;

    RuntimeError: one of the variables needed for gradient computation has been modified by an inplace o ...

  9. CSS动画实例:图文切换

    先准备好一张图片,在页面中放置一个类名为container的层作为图文容器,在该层中再定义两个层:一个类名为image-box的层放置图片,一个类名为text-desc的层放置文本描述,HTML代码描 ...

  10. SwaggerUI看烦了,IGeekFan.AspNetCore.Knife4jUI 帮你换个新皮肤

    背景 好像是上周四,看到微信群有人说java有轮子swagger-bootstrap-ui,而c#,就是找不到. 于是我一看,就说大话:"这个只是一套UI,他这个有开源地址么" 被 ...