@staticmethod 静态方法

函数修饰符,用来修饰一个函数,类似于装饰器

class Dog(object):
def __init__(self,name):
self.name = name def eat(self,food):
print('%s is eating %s'%(self.name,food)) d = Dog('二哈')
d.eat('包子') #二哈 is eating 包子

eat()方法上面加上 @staticmethod

class Dog(object):
def __init__(self,name):
self.name = name
@staticmethod
def eat(self,food):
print('%s is eating %s'%(self.name,food)) d = Dog('二哈')
d.eat('包子') #TypeError: eat() missing 1 required positional argument: 'food'

提示 food 少传递一个参数

删除food参数尝试下

class Dog(object):
def __init__(self,name):
self.name = name
@staticmethod
def eat(self):
print('%s is eating %s'%(self.name,'包子')) d = Dog('二哈')
d.eat() #TypeError: eat() missing 1 required positional argument: 'self'

提示少一个位置参数self ,self默认不是自动传进去的吗,静态方法就是截断方法与类的联系,就是说eat在这里就只是一个单纯的函数。

调用方式:类名。静态方法() 也可以用对象调用方法的方式。

class Dog(object):
def __init__(self,name):
self.name = name
@staticmethod
def eat():
print('%s is eating %s'%('5545','包子')) d = Dog('二哈')
d.eat() #5545 is eating 包子
Dog.eat() #5545 is eating 包子

要在静态方法里面调用self,就必须把self本身传进去

class Dog(object):
def __init__(self,name):
self.name = name
@staticmethod
def eat(self):
print('%s is eating %s'%(self.name,'包子')) d = Dog('二哈')
d.eat(d) #二哈 is eating 包子

静态方法名义上归类管理,实际上调用不了类或者实例的任何属性。

@classmethod  类方法

函数修饰符,同样用来装饰函数

class Dog(object):
def __init__(self,name):
self.name = name
@classmethod
def eat(self):
print('%s is eating %s'%(self.name,'包子')) d = Dog('二哈')
d.eat() #AttributeError: type object 'Dog' has no attribute 'name'

尝试访问类变量

class Dog(object):
name= '秋田'
def __init__(self,name):
self.name = name
@classmethod
def eat(self):
print('%s is eating %s'%(self.name,'包子')) d = Dog('二哈')
d.eat() #秋田 is eating 包子

类方法只能访问类变量,不能访问实例变量。

property 属性方法

class Dog(object):
name= '秋田'
def __init__(self,name):
self.name = name
@property
def eat(self):
print('%s is eating %s'%(self.name,'包子')) d = Dog('二哈')
d.eat() #TypeError: 'NoneType' object is not callable

去掉括号

class Dog(object):
name= '秋田'
def __init__(self,name):
self.name = name
@property
def eat(self):
print('%s is eating %s'%(self.name,'包子')) d = Dog('二哈')
d.eat #二哈 is eating 包子

把一个方法变成静态属性

作为一个属性,如果有参数要怎么传递?

既然是属性就是可以赋值的。

class Dog(object):
name= '秋田'
def __init__(self,name):
self.name = name
@property
def eat(self,food):
print('%s is eating %s'%(self.name,food)) d = Dog('二哈')
d.eat = '包子' #AttributeError: can't set attribute

直接赋值也是不可以的,同样需要装饰下

class Dog(object):
name= '秋田'
def __init__(self,name):
self.name = name
self._food = None
@property
def eat(self):
print('%s is eating %s'%(self.name,self._food))
@eat.setter
def eat(self,food):
print('set food is',food)
self._food = food d = Dog('二哈')
d.eat = '包子' #set food is 包子
d.eat #二哈 is eating 包子

属性赋值会触发 @eat.setter 下的 eat()方法。

删除属性

class Dog(object):
name= '秋田'
def __init__(self,name):
self.name = name
self.__food = None
@property
def eat(self):
print('%s is eating %s'%(self.name,self._food))
@eat.setter
def eat(self,food):
print('set food is',food)
self.__food = food d = Dog('二哈')
d.eat = '包子'
d.eat
del d.eat #AttributeError: 'Dog' object has no attribute '_food'

默认是不能删除的,如果非要删除就要重写

class Dog(object):
name= '秋田'
def __init__(self,name):
self.name = name
self.__food = None
@property
def eat(self):
print('%s is eating %s'%(self.name,self.__food))
@eat.setter
def eat(self,food):
print('set food is',food)
self.__food = food @eat.deleter
def eat(self):
del self.__food
print('删除成功') d = Dog('二哈')
d.eat = '包子' set food is 包子
d.eat # 二哈 is eating 包子
del d.eat # 删除成功
d.eat # AttributeError: 'Dog' object has no attribute '_Dog__food'

d.eat调用了@property下的eat  因为这个方法里面有调用self.__food 而这个属性被删除了

有些场景不能简单的通过定义静态属性来实现的。所有要把方法做成属性。比如有些接口的API。

staticmethod classmethod property方法的更多相关文章

  1. Python staticmethod classmethod 普通方法 类变量 实例变量 cls self 概念与区别

    类变量 1.需要在一个类的各个对象间交互,即需要一个数据对象为整个类而非某个对象服务. 2.同时又力求不破坏类的封装性,即要求此成员隐藏在类的内部,对外不可见. 3.有独立的存储区,属于整个类.   ...

  2. python 类中staticmethod,classmethod,普通方法

    1.staticmethod:静态方法和全局函数类似,但是通过类和对象调用. 2.classmethod:类方法和类相关的方法,第一个参数是class对象(不是实例对象).在python中class也 ...

  3. 【面试必问】python实例方法、类方法@classmethod、静态方法@staticmethod和属性方法@property区别

    [面试必问]python实例方法.类方法@classmethod.静态方法@staticmethod和属性方法@property区别 1.#类方法@classmethod,只能访问类变量,不能访问实例 ...

  4. 封装和 property方法

    封装其实就是一个类用双下划线把自己的属性或者方法给限制住 不让其他的类直接调用或者修改  必须通过这个类来进行操作,这个类通过双下划线__把自己的属性和方法给限制住了 封装就是私有的过程 把父类中的属 ...

  5. staticmethod classmethod修饰符

    一.staticmethod(function) Return a static method for function.A static method does not receive an imp ...

  6. OC中property方法的使用

    我们直入主题,关于property方法,我们先来了解一下相关的知识,首先是成员变量,实例变量,属性变量. 我们定义一个类来看一下 @interface Person :NSObject{ NSInte ...

  7. Python 利用@property装饰器和property()方法将一个方法变成属性调用

    在创建实例属性时,如果直接把实例属性暴露出去,虽然写起来简单,但是存在一些风险,比如实例属性可以在外部被修改. 为了限制外部操作,可以通过一个set_score()方法来设置成绩,再通过一个get_s ...

  8. python静态属性@property、类方法@classmethod、静态方法@staticmethod和普通方法

    静态属性:即将类的函数通过@property属性封装,封装后实例调用该函数时,不再需要在函数后面加(),而是用类似调用数据属性的方式直接调用函数名称即可执行函数. 静态属性既可以访问类的属性,也可以访 ...

  9. python staticmethod,classmethod方法的使用和区别以及property装饰器的作用

    class Kls(object): def __init__(self, data): self.data = data def printd(self): print(self.data) @st ...

随机推荐

  1. [转载]sscanf函数

    来源:http://c.biancheng.net/cpp/html/296.html 头文件:#include <stdio.h> sscanf()函数用于从字符串中读取指定格式的数据, ...

  2. PXE刷机,存储节点失败

    最近刚刚帮客户对一台满配的X6-2刷机初始化,尝试了下PXE方式,但刷完机后,发现计算节点的imagehistory输出的状态都是成功的,而所有的存储节点状态都为failure,具体如下: [root ...

  3. winfom实现关闭后一直运行

    using PLog; using System; using System.Collections.Generic; using System.Diagnostics; using System.L ...

  4. Codeforces 277E

    按边建模,二叉树一条入边两条出边 判断就要用到mcmf的好处了 #include<bits/stdc++.h> using namespace std; const int maxn = ...

  5. hdu1698 线段树(区间更新~将区间[x,y]的值替换为z)

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. LeeCode(No3 - Longest Substring Without Repeating Characters)

    题目: Given a string, find the length of the longest substring without repeating characters. 示例: Given ...

  7. 缓存算法及Redis、Memcached、Guava、Ehcache中的算法

    https://my.oschina.net/ffy/blog/501003 https://yq.aliyun.com/articles/622757 https://blog.csdn.net/s ...

  8. java登录验证码 用到spring框架

    转载:https://blog.csdn.net/zqd_java/article/details/53638143 在次大神基础上添加下述js代码即可使用了. //登陆验证 function cha ...

  9. spring test: 配置文件优先级

    application.properties 默认 application-xxx.properties 高 systemEnvironment 高 test/main/resources/ 同名文件 ...

  10. Linux 添加用户(user),组(Group)以及权限(Permission)

    1. 添加用户 sudo adduser UserName 异常: sudo adduser --force-badname <username> 之后为这个用户添加其他辅助信息 切换用户 ...