python 类中staticmethod,classmethod,普通方法

1.staticmethod:静态方法
和全局函数类似,但是通过类和对象调用。

2.classmethod:类方法
和类相关的方法,第一个参数是class对象(不是实例对象)。
在python中class也是一个真实存在于内存中的对象,不同于其他语言只存在于编译期间。

3.普通方法
和实例相关的方法,通过类实例调用。

4.代码示例

#coding:utf-8
‘‘‘
Created on 2015年5月29日
@author: canx
‘‘‘

class Person:
    def __init__(self):
        print "init"

    @staticmethod
    def sayHello(hello):
        print "sayHell %s"%(hello)

    @classmethod
    def introduce(cls,hello):
        print "introduce %s"%(hello)

    def hello(self,hello):
        print "hello %s"%(hello)

def main():
    Person.sayHello("test")
    Person.introduce("test")
    p=Person()
    p.hello("test")

if __name__==‘__main__‘:
    main()

输出结果:

时间: 2024-08-28 16:55:11

python 类中staticmethod,classmethod,普通方法的相关文章

python类中几个特殊方法

class TT: def __init__(self): print "__init__" def __call__(self): print "__call__" def __str__(self): return "__str__" def __int__(self): return "__int__" def __add__(self,other): return "__add__" def __s

python类中的一些神奇方法

__str__:用于在print(对象)时,直接打印__str__的返回值 1 class Animal: 2 def __init__(self, name): 3 self.name = name 4 def __str__(self): 5 return self.name 6 7 d = Animal("dog") 8 print(d) __fun_name:私有方法 1 class Animal: 2 def __init__(self, name): 3 self.name

Python 之面向对象:类和对象调用类中的变量和方法

面向对象的核心是对象,用对象来操控类里面的方法和变量,加上类还具有继承.封装.多态三大特性,提高了代码的复用性和规范性. 一.对象 调用类中的变量和方法 二 .类  调用类中的变量和方法 原文地址:https://www.cnblogs.com/tianpin/p/11283032.html

Python类中实例属性的通用显示工具

0.说明 以下的思想方法非常有用,可以帮助你在Python开发提高开发和维护效率,所以可能的话,请仔细琢磨一下其中的代码. 之前在用Python编写一个类时,为了显示的友好性,总是需要在每个类中重载__str__或者__repr__方法,现在有了更好的方法,不需要在每个类中都这么做了,下面给出的方法非常实用. 下面使用的例子使用的Python版本都是Python3.5,但实际上在Python2.7中进行也没有任何影响. 1.正常情况下类实例的不友好显示 在Python中编写一个类时,由于没有重载

Python类属性访问的魔法方法

Python类属性访问的魔法方法: 1. __getattr__(self, name)- 定义当用户试图获取一个不存在的属性时的行为 2. __getattribute__(self, name)- 定义当该类的属性被访问时的行为 注意:当__getattr__与__getattribute__同时重写时,访问属性时,优先调用__getattribute__,只有当被访问的属性不存在时才触发__getattr__ 3. __setattr__(self, name, value)- 定义当一个

[python] 类常用的内置方法

内置方法 说明 __init__(self,...) 初始化对象,在创建新对象时调用 __del__(self) 释放对象,在对象被删除之前调用 __new__(cls,*args,**kwd) 实例的生成操作 __str__(self) 在使用print语句时被调用 __getitem__(self,key) 获取序列的索引key对应的值,等价于seq[key] __len__(self) 在调用内联函数len()时被调用 __cmp__(stc,dst) 比较两个对象src和dst __ge

python类与对象各个魔法方法总结

1.python类与对象各个魔法方法总结: 2.各个魔法方法应用举例: 3.实例训练: (1)我们都知道在 Python 中,两个字符串相加会自动拼接字符串,但遗憾的是两个字符串相减却抛出异常.因此,现在我们要求定义一个 Nstr 类,支持字符串的相减操作:A – B,从 A 中去除所有 B 的子字符串. class Nstr(str): def __sub__(self,other):  self=list(self)         other=list(other) for i in ot

增加、删除类文件或者在一个类中增加、删除方法时,是不能够热部署到服务上的。这时候需要停止服务器重新部署后再启动,就不会出现上面的提示了。

Hot Code Replace Failed 2010-11-05 10:11listquiry | 浏览 14226 次 Some code changes cannot be hot swapped into a running virtual machine, such as changing method names or introducing errors into running code.The current target virtual machine {jboss4Ser

php面向对象类中常用的魔术方法

php面向对象类中常用的魔术方法 1.__construct():构造方法,当类被实例化new $class时被自动调用的方法,在类的继承中可以继承与覆盖该方法,例: //__construct() class construct{ public function __construct(){ $this->var = "this is var"; } } class con2 extends construct{ public function __construct(){ $