Python staticmethod
1 @staticmethod 静态方法
when this method is called, we don't pass an instance of the class to it (as we normally do with methods).
This means you can put a function inside a class but you can't access the instance of that class (this is useful when your method does not use the instance).
当这个方法被调用时,我们不会将类的实例传递给它(就像我们通常用方法做的那样)。
这意味着你可以在一个类中放入一个函数,但是你不能访问该类的实例(当你的方法不使用实例时这很有用)
关于参数:该方法不强制要求传递参数,(如不需要表示自身对象的self和自身类的cls参数,就跟使用函数一样。)
如下声明一个静态方法:
class C(object):
@staticmethod
def f(arg1, arg2, ...):
...
以上实例声明了静态方法 f,类可以不用实例化就可以调用该方法 C.f(),当然也可以实例化后调用 C().f()。
函数语法:staticmethod(function)
例如:
#!/usr/bin/python
# -*- coding: UTF-8 -*- class C(object):
@staticmethod
def f():
print('runoob'); C.f(); # 静态方法无需实例化
cobj = C()
cobj.f() # 也可以实例化后调用
又如:
# 类中
@staticmethod
def is_date_valid(date_as_string):
day, month, year = map(int, date_as_string.split('-'))
return day <= 31 and month <= 12 and year <= 3999 # usage:
is_date = Date.is_date_valid('11-09-2012')
这里结果(is_date)是True/False
2 @classmethod 类方法
when this method is called, we pass the class as the first argument instead of the instance of that class (as we normally do with methods).
This means you can use the class and its properties inside that method rather than a particular instance.
当调用此方法时,我们将该类作为第一个参数传递,而不是该类的实例(正如我们通常对方法所做的那样)。
这意味着您可以在该方法内使用该类及其属性,而不能使用特定的实例。
关于参数:该方法强制要求传递一个必须参数, 不需要self参数,但第一个参数需要是表示自身类的cls参数。
如:
#!/usr/bin/python
# -*- coding: UTF-8 -*- class A(object):
bar = 1
def func1(self):
print ('foo')
@classmethod
def func2(cls):
print ('func2')
print (cls.bar)
cls().func1() # 调用 foo 方法 A.func2() # 不需要实例化
例如:
# 类中
@classmethod
def from_string(cls, date_as_string):
day, month, year = map(int, date_as_string.split('-'))
date1 = cls(day, month, year)
return date1 date2 = Date.from_string('11-09-2012')
这里结果(date2)是一个Date的实例。
他有如下好处:
- We've implemented date string parsing in one place and it's reusable now.
- Encapsulation works fine here (if you think that you could implement string parsing as a single function elsewhere, this solution fits OOP paradigm far better).
clsis an object that holds class itself, not an instance of the class. It's pretty cool because if we inherit ourDateclass, all children will havefrom_stringdefined also.
简单来说就是,可以有无限的from_string方法。
3 实例方法
4 最后:
使用@classmethod或@staticmethod都可以类名.方法名()调用函数
前者必须要一个类参数,后者可以不必须要参数
class Date(object):
def __init__(self, day=0, month=0, year=0):
self.day = day
self.month = month
self.year = year
@classmethod
def from_string(cls, date_as_string):
day, month, year = map(int, date_as_string.split('-'))
date1 = cls(day, month, year)
return date1
@staticmethod
def is_date_valid(date_as_string):
day, month, year = map(int, date_as_string.split('-'))
return day <= 31 and month <= 12 and year <= 3999
date2 = Date.from_string('11-09-2012')
is_date = Date.is_date_valid('11-09-2012')
Python staticmethod的更多相关文章
- Python staticmethod() 函数
Python staticmethod() 函数 Python 内置函数 python staticmethod 返回函数的静态方法. 该方法不强制要求传递参数,如下声明一个静态方法: class ...
- python staticmethod classmethod
http://www.cnblogs.com/chenzehe/archive/2010/09/01/1814639.html classmethod:类方法staticmethod:静态方法 在py ...
- Python staticmethod classmethod 普通方法 类变量 实例变量 cls self 概念与区别
类变量 1.需要在一个类的各个对象间交互,即需要一个数据对象为整个类而非某个对象服务. 2.同时又力求不破坏类的封装性,即要求此成员隐藏在类的内部,对外不可见. 3.有独立的存储区,属于整个类. ...
- python staticmethod,classmethod方法的使用和区别以及property装饰器的作用
class Kls(object): def __init__(self, data): self.data = data def printd(self): print(self.data) @st ...
- python @staticmethod和@classmethod
Python其实有3个方法,即 静态方法 (staticmethod), 类方法 (classmethod)和 实例方法. 如下: def foo(x): print "executing ...
- python staticmethod和classmethod(转载)
staticmethod, classmethod 分别被称为静态方法和类方法. staticmethod 基本上和一个全局函数差不多,只不过可以通过类或类的实例对象(python里只说对象总是容易产 ...
- python staticmethod&classmethod
python中的这两种方法都通过修饰器来完成 静态方法: 不需要传递类对象或者类的实例 可以通过类的实例.方法名a().foo()或者类名.方法a.foo()名来访问 当子类继承父类时,且实例化子类时 ...
- python staticmethod and classmethod方法
静态方法无绑定,和普通函数使用方法一样,只是需要通过类或者实例来调用.没有隐性参数. 实例方法针对的是实例,类方法针对的是类,他们都可以继承和重新定义,而静态方法则不能继承,可以认为是全局函数. #h ...
- python @staticmethod和@classmethod的作用
一般来说,要使用某个类的方法,需要先实例化一个对象再调用方法. 而使用@staticmethod或@classmethod,就可以不需要实例化,直接类名.方法名()来调用. 这有利于组织代码,把某些应 ...
随机推荐
- 巨蟒python全栈开发django14:Form组件
1.form组件初识 2.常用字段测试 3.局部钩子和全局钩子 4.简单看源码
- NoSQL 数据库分类 颠覆
NoSQL 数据库分类 类型 部分代表特点 列存储 HbaseCassandraHypertable顾名思义,是按列存储数据的.最大的特点是方便存储结构化和半结构化数据,方便做数据压缩,对针对某一列或 ...
- SQLServer中行列转换Pivot UnPivot
PIVOT用于将列值旋转为列名(即行转列),在SQL Server 2000可以用聚合函数配合CASE语句实现 PIVOT的一般语法是:PIVOT(聚合函数(列) FOR 列 in (…) )AS P ...
- Django 之 URL(路由)分发机制
本质 (1):它的本质是 URL 模式以及要为该 URL 模式调用的视图函数之间的映射表. django-admin.py startproject 运行时,该脚本会自动为你建了一份 URLconf( ...
- 0x05 MySQL 数据操作
一 插入数据INSERT 1. 插入完整数据(顺序插入) 语法一: INSERT INTO 表名(字段1,字段2,字段3…字段n) VALUES(值1,值2,值3…值n); 语法二: INSERT I ...
- springMVC文件的上传与下载
1.文件上传 springmvc中只需要配置上传组件,然后配合使用MultipartFile,就可以轻松实现单个文件上传和批量上传,而且上传的文件类型和大小都可以在springmvc 配置文件中配置. ...
- springboot——数据层访问搭建 集成Duid连接池
springboot中默认是使用的tomcat的连接池,如果我们想要第三方的连接池,我们这么配置呢? 首先在application.yml文件中注释掉之前数据库的配置,重新用druid的方式配置: # ...
- linux下从源代码安装git的问题(install from source)
安装环境:centos7.2 安装依赖包: yum install -y gcc .el7..x86_64 openssl-devel.x86_64 yum install -y curl.x86_6 ...
- Python编程-网络编程进阶(IO复用、Socketserver)
一.认证客户端的链接合法性 如果你想在分布式系统中实现一个简单的客户端链接认证功能,又不像SSL那么复杂,那么利用hmac+加盐的方式来实现. 服务端 from socket import * imp ...
- 正则表达式 获取字符串内提取图片URL字符串
#region 获取字符串内提取图片URL字符串 /// <summary> /// 获取字符串内提取图片URL字符串 /// </summary> /// <param ...