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的实例。

他有如下好处:

  1. We've implemented date string parsing in one place and it's reusable now.
  2. 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).
  3. cls is an object that holds class itself, not an instance of the class. It's pretty cool because if we inherit our Date class, all children will have from_string defined 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的更多相关文章

  1. Python staticmethod() 函数

    Python staticmethod() 函数  Python 内置函数 python staticmethod 返回函数的静态方法. 该方法不强制要求传递参数,如下声明一个静态方法: class ...

  2. python staticmethod classmethod

    http://www.cnblogs.com/chenzehe/archive/2010/09/01/1814639.html classmethod:类方法staticmethod:静态方法 在py ...

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

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

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

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

  5. python @staticmethod和@classmethod

    Python其实有3个方法,即 静态方法 (staticmethod), 类方法 (classmethod)和 实例方法. 如下: def foo(x): print "executing ...

  6. python staticmethod和classmethod(转载)

    staticmethod, classmethod 分别被称为静态方法和类方法. staticmethod 基本上和一个全局函数差不多,只不过可以通过类或类的实例对象(python里只说对象总是容易产 ...

  7. python staticmethod&classmethod

    python中的这两种方法都通过修饰器来完成 静态方法: 不需要传递类对象或者类的实例 可以通过类的实例.方法名a().foo()或者类名.方法a.foo()名来访问 当子类继承父类时,且实例化子类时 ...

  8. python staticmethod and classmethod方法

    静态方法无绑定,和普通函数使用方法一样,只是需要通过类或者实例来调用.没有隐性参数. 实例方法针对的是实例,类方法针对的是类,他们都可以继承和重新定义,而静态方法则不能继承,可以认为是全局函数. #h ...

  9. python @staticmethod和@classmethod的作用

    一般来说,要使用某个类的方法,需要先实例化一个对象再调用方法. 而使用@staticmethod或@classmethod,就可以不需要实例化,直接类名.方法名()来调用. 这有利于组织代码,把某些应 ...

随机推荐

  1. H - N皇后问题

    H - N皇后问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descripti ...

  2. SharePoint服务器端对象模型 之 使用CAML进行数据查询(Part 2)

    (三)使用SPQuery进行列表查询 1.概述 列表查询主要是指在一个指定的列表(或文档库)中按照某些筛选.排序条件进行查询.列表查询主要使用SPQuery对象,以及SPList的GetItems方法 ...

  3. Java中Solr集群的测试

    import org.apache.solr.client.solrj.impl.CloudSolrServer; import org.apache.solr.common.SolrInputDoc ...

  4. js验证表单大全3

    2 >表单提交验证类  2.1 表单项不能为空 <scriptlanguage="javascript"> <!-- function CheckForm( ...

  5. Python获取位数

    import platform platform.architecture()

  6. Java基础—运算符(转载)

    转载自:Java运算符 计算机的最基本用途之一就是执行数学运算,作为一门计算机语言,Java也提供了一套丰富的运算符来操纵变量.我们可以把运算符分成以下几组: 算术运算符 关系运算符 位运算符 逻辑运 ...

  7. HAProxy的访问控制

    HAProxy的ACL用于实现基于请求报文首部.响应报文的内容或其他的环境状态信息来做出转发决策,这大大增强了其配置弹性,其配置法则通常分为两步,首先去定义ACL,即定义一个测试条件,而后在条件得到满 ...

  8. 系统日志服务rsyslog

    一.系统日志服务rsyslog:多线程,可以基于UDP.TCP.TLS协议进行远程通信,还可以将数据存储到MySQL.PGSQL.Oracle,强大的过滤器,可实现过滤日志信息中任何部分,可以自定义输 ...

  9. Python爬虫:爬取自己博客的主页的标题,链接,和发布时间

    代码 # -*- coding: utf-8 -*- """ ------------------------------------------------- File ...

  10. 剑指offer 面试38题

    面试38题: 题:字符串的排列 题目:输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,ca ...