python pro_python_@propetry

@propetry的作用就是让一个方法可以当成属性被调用。

@property的实现比较复杂,我们先考察如何使用。把一个getter方法变成属性,只需要加上@property就可以了,此时,@property本身又创建了另一个装饰器@score.setter,负责把一个setter方法变成属性赋值,于是,我们就拥有一个可控的属性操作

class Rectangle():

@property

def width(self):

return self._width

@width.setter

def width(self, value):

if not isinstance(value,int):

raise ValueError('be int!')

if value < 0 or value > 100:

raise ValueError('valueError')

self._width = value

@property

def squer(self):

return self._width * self._width

s = Rectangle()

s.width = 20 #width这个属性是可读可写的,写入时还会检查

print(s.squer) #squer这个属性时只读的,无法写入,会报错。

一个例子:

# 使用@property装饰器,data会被self.data这个属性调用,返回_data

# 这样可以对参数进行检查

@property

def data(self):

# is_valid会把initial_data替换为_validated_data

# 这个if防止没有调用is_valid

if hasattr(self, 'initial_data') and not hasattr(self, '_validated_data'):

msg = (

'When a serializer is passed a `data` keyword argument you '

'must call `.is_valid()` before attempting to access the '

'serialized `.data` representation.\n'

'You should either call `.is_valid()` first, '

'or access `.initial_data` instead.'

)

raise AssertionError(msg)

# 设定返回值_data

if not hasattr(self, '_data'):

if self.instance is not None and not getattr(self, '_errors', None):

self._data = self.to_representation(self.instance)

elif hasattr(self, '_validated_data') and not getattr(self, '_errors', None):

self._data = self.to_representation(self.validated_data)

else:

self._data = self.get_initial()

return self._data

# 参数检查:没有_errors,则self.errors调用就会出错

@property

def errors(self):

if not hasattr(self, '_errors'):

msg = 'You must call `.is_valid()` before accessing `.errors`.'

raise AssertionError(msg)

return self._errors

# 参数检查:没有_validated_data,则self.validated_data调用就会出错

@property

def validated_data(self):

if not hasattr(self, '_validated_data'):

msg = 'You must call `.is_valid()` before accessing `.validated_data`.'

raise AssertionError(msg)

return self._validated_data

内容简介: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、付费专栏及课程。

余额充值