前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python 学习入门(37)—— @classmethod函数

Python 学习入门(37)—— @classmethod函数

作者头像
阳光岛主
发布2019-02-18 16:16:17
3600
发布2019-02-18 16:16:17
举报
文章被收录于专栏:米扑专栏

@classmethod : 类方法

@staticmethod : 静态方法

类方法和静态方法的调用一样,都是通过类就可以直接调用。

区别:类方法,需要传入该类,定义类方法的时候要传一个默认的参数cls。静态方法则不用。

示例:

代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# blog.ithomer.net

class Test(object):
    x = 11
    def __init__(self, _x):
        self._x = _x
        print("Test.__init__")
 
    @classmethod
    def class_method(cls):
        print("class_method")
 
    @staticmethod
    def static_method():
        print("static_method")
 
    @classmethod
    def getPt(cls):
        cls.class_method()
        cls.static_method()
 
if "__main__" == __name__:
    Test.class_method()         # class_method
    Test.static_method()        # static_method
    Test.getPt()                # class_method  static_method

    t = Test(22)                # Test.__init__
    t.class_method()            # class_method
    t.static_method()           # static_method
    
    print Test.x                # 11
#     print Test._x
    
    print t.x                   # 11
    print t._x                  # 22
    
#     t.getPr()   # 'Test' object has no attribute 'getPr'

运行结果:

代码语言:javascript
复制
class_method
static_method
class_method
static_method
Test.__init__
class_method
static_method
11
11
22
Traceback (most recent call last):
  File "/home/homer/workspace/myPython/com/connmiliao.py", line 40, in <module>
    t.getPr() 
AttributeError: 'Test' object has no attribute 'getPr'

示例:@property,@staticmethod,@classmethod 

代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# blog.ithomer.net

class MyClass(object):
    def __init__(self):
        print '__init__'
        self._name = 'blog.ithomer.net'

    @staticmethod
    def static_method():
        print 'This is a static method!'

    def test(self):
        print 'call test'

    @classmethod
    def class_method(cls):
        print 'cls: ',cls
        print 'cls.name: ',cls.name
        print 'cls.static_method(): ',cls.static_method()
        instance = cls()
        print 'instance.test(): ',instance.test()

    @property
    def name(self):
        return self._name
    
    @name.setter
    def name(self, value):
        self._name = value

if __name__ == '__main__':
    MyClass.static_method()
    MyClass.class_method()
    
    mc = MyClass()
    print mc.name
    mc.name = 'forum.ithomer.net'  
    print mc.name

运行结果:

This is a static method! cls:  <class '__main__.MyClass'> cls.name:  <property object at 0x150d680> cls.static_method():  This is a static method! None __init__ instance.test():  call test None __init__ blog.ithomer.net forum.ithomer.net

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014年02月21日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
日志服务
日志服务(Cloud Log Service,CLS)是腾讯云提供的一站式日志服务平台,提供了从日志采集、日志存储到日志检索,图表分析、监控告警、日志投递等多项服务,协助用户通过日志来解决业务运维、服务监控、日志审计等场景问题。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档