python中的pro什么意思_python内置函数proprety()

property 可以将python定义的函数“当做”属性访问,从而提供更加友好访问方式,但是有时候setter/getter也是需要的

假设定义了一个类Cls,该类必须继承自object类,有一私有变量__x

1. 第一种使用属性的方法:class Cls(object):

def __init__(self):

self.__x = None

def getx(self):

return self.__x

def setx(self, value):

self.__x = value

def delx(self):

del self.__x

x = property(getx, setx, delx, 'set x property')

if __name__ == '__main__':

c = Cls()

c.x = 100

y = c.x

print("set & get y: %d" % y)

del c.x

print("del c.x & y: %d" % y)运行结果:

set & get y: 100

del c.x & y: 100

在该类中定义三个函数,分别用作赋值、取值、删除变量

property函数原型为property(fget=None,fset=None,fdel=None,doc=None),上例根据自己定义相应的函数赋值即可。

2. 第二种方法(在2.6中新增)同方法一,首先定义一个类Cls,该类必须继承自object类,有一私有变量__xclass Cls(object):

def __init__(self):

self.__x = None

@property

def x(self):

return self.__x

@x.setter

def x(self, value):

self.__x = value

@x.deleter

def x(self):

del self.__x

if __name__ == '__main__':

c = Cls()

c.x = 100

y = c.x

print("set & get y: %d" % y)

del c.x

print("del c.x & y: %d" % y)运行结果:

set & get y: 100

del c.x & y: 100

说明: 同一属性__x的三个函数名要相同。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
内容简介:You've learned the basics of Python, but how do you take your skills to the next stage? Even if you know enough to be productive, there are a number of features that can take you to the next level in Python. Pro Python explores concepts and features normally left to experimentation, allowing you to be even more productive and creative. In addition to pure code concerns, Pro Python will develop your programming techniques and approaches, which will help make you a better Python programmer. Not only will this book help your code, it will also help you understand and interact with the many established Python communities, or even start your own. * Take your Python knowledge and coding skills to the next level. * Write clean, innovative code that will be respected among your peers. * Make your code do more with introspection and metaprogramming. * Design complete frameworks and libraries (two are included in the book!). What you'll learn * Write strong Python code that will be respected in the Python community. * Understand the reasons behind big design decisions in Python. * Write programs that can reconfigure themselves in Python. * Disguise your code as different types of objects in Python. * Inspect just about any object in Python. * Prepare your code for international audiences. * Ensure code quality with rigorous testing. Who this book is for This book is for intermediate to advanced Python programmers who are looking to understand how and why Python works the way it does and how they can take their code to the next level. Table of Contents * Principles and Philosophy * Advanced Basics * Functions * Classes * Common Protocols * Object Management * Strings * Documentation * Testing * Distribution * Sheets: A CSV Framework
Pro Python 第2版,2014.12.17出版的新书 You’ve learned the basics of Python, but how do you take your skills to the next stage? Even if you know enough to be productive, there are a number of features that can take you to the next level in Python. Pro Python, Second Edition explores concepts and features normally left to experimentation, allowing you to be even more productive and creative. In addition to pure code concerns, Pro Python develops your programming techniques and approaches, which will help make you a better Python programmer. This book will improve not only your code but also your understanding and interaction with the many established Python communities. This book takes your Python knowledge and coding skills to the next level. It shows you how to write clean, innovative code that will be respected by your peers. With this book, make your code do more with introspection and meta-programming. And learn and later use the nuts and bolts of an application, tier-by-tier as a complex case study along the way. This book is for intermediate to advanced Python programmers who are looking to understand how and why Python works the way it does and how they can take their code to the next level. Table of Contents 1. Principles and Philosophy 2. Advanced Basics 3. Functions 4. Classes 5. Common Protocols 6. Object Management 7. Strings 8. Documentation 9. Testing 10. Distribution 11. Sheets: A CSV Framework 12. Style Guide for Python 13. Voting Guidelines 14. The Zen of Python 15. Docstring Conventions 16. Backward Compatibility Policy 17. Python 3000 18. Python Language Moratorium

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值