python面试题之下面这些是什么意思:@classmethod, @staticmethod, @property?
回答背景知识
这些都是装饰器(decorator)。装饰器是一种特殊的函数,要么接受函数作为输入参数,并返回一个函数,要么接受一个类作为输入参数,并返回一个类。
@标记是语法糖(syntactic sugar),可以让你以简单易读得方式装饰目标对象。
@my_decorator def my_func(stuff):do_things Is equivalent todef my_func(stuff):do_thingsmy_func = my_decorator(my_func)
你可以在本网站上找到介绍装饰器工作原理的教材。
真正的答案
@classmethod
, @staticmethod
和@property
这三个装饰器的使用对象是在类中定义的函数。下面的例子展示了它们的用法和行为:
class MyClass(object):def __init__(self):self._some_property = "properties are nice"self._some_other_property = "VERY nice"def normal_method(*args,**kwargs):print "calling normal_method({0},{1})".format(args,kwargs)@classmethoddef class_method(*args,**kwargs):print "calling class_method({0},{1})".format(args,kwargs)@staticmethoddef static_method(*args,**kwargs):print "calling static_method({0},{1})".format(args,kwargs)@propertydef some_property(self,*args,**kwargs):print "calling some_property getter({0},{1},{2})".format(self,args,kwargs)return self._some_property@some_property.setterdef some_property(self,*args,**kwargs):print "calling some_property setter({0},{1},{2})".format(self,args,kwargs)self._some_property = args[0]@propertydef some_other_property(self,*args,**kwargs):print "calling some_other_property getter({0},{1},{2})".format(self,args,kwargs)return self._some_other_propertyo = MyClass() # 未装饰的方法还是正常的行为方式,需要当前的类实例(self)作为第一个参数。o.normal_method # <bound method MyClass.normal_method of <__main__.MyClass instance at 0x7fdd2537ea28>>o.normal_method() # normal_method((<__main__.MyClass instance at 0x7fdd2537ea28>,),{})o.normal_method(1,2,x=3,y=4) # normal_method((<__main__.MyClass instance at 0x7fdd2537ea28>, 1, 2),{'y': 4, 'x': 3})# 类方法的第一个参数永远是该类o.class_method # <bound method classobj.class_method of <class __main__.MyClass at 0x7fdd2536a390>>o.class_method() # class_method((<class __main__.MyClass at 0x7fdd2536a390>,),{})o.class_method(1,2,x=3,y=4) # class_method((<class __main__.MyClass at 0x7fdd2536a390>, 1, 2),{'y': 4, 'x': 3})# 静态方法(static method)中除了你调用时传入的参数以外,没有其他的参数。o.static_method # <function static_method at 0x7fdd25375848>o.static_method() # static_method((),{})o.static_method(1,2,x=3,y=4) # static_method((1, 2),{'y': 4, 'x': 3})# @property是实现getter和setter方法的一种方式。直接调用它们是错误的。 # “只读”属性可以通过只定义getter方法,不定义setter方法实现。o.some_property # 调用some_property的getter(<__main__.MyClass instance at 0x7fb2b70877e8>,(),{}) # 'properties are nice' # “属性”是很好的功能o.some_property() # calling some_property getter(<__main__.MyClass instance at 0x7fb2b70877e8>,(),{}) # Traceback (most recent call last): # File "<stdin>", line 1, in <module> # TypeError: 'str' object is not callableo.some_other_property # calling some_other_property getter(<__main__.MyClass instance at 0x7fb2b70877e8>,(),{}) # 'VERY nice'# o.some_other_property() # calling some_other_property getter(<__main__.MyClass instance at 0x7fb2b70877e8>,(),{}) # Traceback (most recent call last): # File "<stdin>", line 1, in <module> # TypeError: 'str' object is not callableo.some_property = "groovy" # calling some_property setter(<__main__.MyClass object at 0x7fb2b7077890>,('groovy',),{})o.some_property # calling some_property getter(<__main__.MyClass object at 0x7fb2b7077890>,(),{}) # 'groovy'o.some_other_property = "very groovy" # Traceback (most recent call last): # File "<stdin>", line 1, in <module> # AttributeError: can't set attributeo.some_other_property # calling some_other_property getter(<__main__.MyClass object at 0x7fb2b7077890>,(),{})
本文首发于Python黑洞网,博客园同步跟新
python面试题之下面这些是什么意思:@classmethod, @staticmethod, @property?相关推荐
- 面试前赶紧看了5道Python Web面试题,Python面试题No17
目录 本面试题题库,由公号:非本科程序员 整理发布 第1题: Flask中的请求上下文和应用上下文是什么? 第2题:django中间件的使用? 第3题: django开发中数据做过什么优化? 第4题: ...
- 21年最新Python面试题及答案汇总详解(上)
错过三月找工作的机会,还要错过四月的好时期吗?Python面试你做准备了吗?下面小编整理了一套2021年最新Python常见面试题目,及Python面试题目答案汇总.希望能够帮助到大家. 21年最新P ...
- 300道Python面试题,备战春招!
作者 | kenwoodjw 责编 | Jane 出品 | Python大本营(ID:pythonnews) 程序员转行学什么语言? https://edu.csdn.net/topic/ai30? ...
- 一道Python面试题,据说大部分人都中招了,纷纷开始怀疑自己
无意间,看到这么一道Py无意间,看到这么一道Python面试题:以下代码将输出什么? def testFun(): temp = [lambda x : i*x for i in range(4)] ...
- python面试题_Python面试题大全
[导读]推荐一个Python面试题大全,从Python基础到Python高级等非常全的面试题讲解. GitHub地址: https://github.com/kenwoodjw/python_inte ...
- python用哪个版本比较好 2020_2020年最常见的Python面试题答案
Python新手在谋求一份Python编程工作前,必须熟知Python的基础知识.编程网站DataFlair的技术团队分享了一份2020年最常见Python面试题合集,既有基本的Python面试题,也 ...
- 三年python面试题_300道Python面试题
原标题:300道Python面试题 Python 工程师也分不同的技术岗位,初级.中级与高级开发工程师需要具备的技能也不同. 然而,无论是零经验还是 Python 老司机,对待每一次面试与笔试,都不能 ...
- python面试题及答案-50道Python面试题集锦(附答案)
原标题:50道Python面试题集锦(附答案) Python是目前编程领域最受欢迎的语言.在本文中,我将总结Python面试中最常见的50个问题.每道题都提供参考答案,希望能够帮助你在2019年求职面 ...
- python面试-2018年最常见的Python面试题答案(上篇)
Python新手在谋求一份Python编程工作前,必须熟知Python的基础知识.编程网站DataFlair的技术团队分享了一份2018年最常见Python面试题合集,既有基本的Python面试题,也 ...
最新文章
- tidb mysql 协议_TiDB源码阅读(二) TiDB中的MySQL协议
- printf输出字符串的一些格式
- 当深度学习遇上量化交易——因子挖掘篇
- java中对象的生存期_JSP中JavaBean的生命周期
- echarts 选中bush中lineX
- html字体代码大全_Vba群发邮件及HTML设置字体格式的代码
- 三星Galaxy Fold中国区发布会临时取消:推迟时间未定 彻查屏幕问题
- python读取xml文件内容_python读取xml文件
- ORB-SLAM2 地图加载2
- ES6-模块导入导出
- Github 2019 年最值得关注的数据科学项目 Virgilio(维吉尔) 中文版
- 资产管理界的风控大师-贝莱德BlackRock集团
- C语言编程怎么搜答案,C语言编程题及答案
- 遗传算法原理与应用详解
- 【项目总结】中国大学生计算机设计(机械制造暨政治正确)大赛
- Eclipse启动Tomcat 警告: 基于APR的本地库加载失败.错误报告为
- 【gp数据库】十条实用数据库SQL优化建议
- HashMap什么时候由链表转红黑树
- echarts关系图vue完整代码
- Docker 的LNMP + Wordpress搭建
热门文章
- mybatis注解开发_Spring Boot 中集成 MyBatis
- docker 仓库镜像 替换_自己动手创建 Docker 镜像并分享到镜像仓库,容器引擎的用途越来越广泛!...
- 利用Pin实现CodeCoverage
- Linux /etc/rc.d 下面 rc${runlevel}.d rc.local init.d 区别
- 电脑硬盘为什么叫计算机,电脑分区为何从C盘开始?
- (52)FPGA基础编码D触发器(一)
- (47)System Verilog数组排序
- STM32网络电路设计
- java servlet 入门_servlet 入门详解
- 【好文链接】环形队列、串口数据处理