Intermediate Python Programming: The Insider Guide to Intermediate Python Programming Concepts by Richard Ozer & Python Programming
Author:Richard Ozer & Python Programming [Ozer, Richard]
Language: eng
Format: azw3
Published: 2017-10-08T04:00:00+00:00
def __delete__(self,instance):
raise AttributeError("Cannot delete the attribute")
class Foo(object):
name = TypedProperty("name",str)
num = TypedProperty("num",int,42)
>> acct = Foo()
>> acct.name = "obi"
>> acct.num = 1234
>> print acct.num
1234
>> print acct.name
obi
# attempting to assign a string to a number doesn’t work
>> acct.num = '1234'
TypeError: This must be a <type 'int'>
So, what we have done here is implement the TypedProperty descriptor. This class will enforce type checking on any attribute of the class it is representing. Do be aware that you can only define a descriptor legally at class level and not at instance level, for example within the __init__ method, as you can see above.
Take the Foo class instance – when we access any attribute of it, the descriptor will call the method __get__(). The first argument of this method will be the object that the attribute represented by the descriptor references. The __set__ method will be called by the descriptor when the attribute is assigned.
To gain a better understanding of the reason why a descriptor may be used for the representation of an object attribute, you first need to understand how Python carries out the reference resolution for attributes. For an object, we use object.__getattribute__() for attribute resolutions. This will turn b.x into type(b).__dict__[‘x’].__get__(b, type(b)). Next, the resolution will use a precedence chain to look for the attribute. This chain provides the data descriptors that are in the class called dict with priority over the instance variables; it gives those instance variables priority over any non-data descriptor and will assign getattr() with the lowest priority. We can override that chain by using customized __getattribute__ methods for the object class.
Once you fully understand how descriptors work, it is so easy to picture better solutions for the remaining two scenarios we gave earlier. By implementing an attribute that is read-only with a descriptor will become no harder than implementing a descriptor that doesn’t have a __set__ method, i.e. a data descriptor. If you wanted to customize access, all you would need to do is add the functionality needed in the __get__ and __set__ methods.
Class Properties
It gets a bit tiresome to have to define these descriptor classes every time we want one so Python has given us a better way of giving an attribute a data descriptor. Have a look at the following property signature:
property(fget=None, fset=None, fdel=None, doc=None) -> property attribute
fdel, fset, and fget are the deleter, setter and getter methods for the class. The following example shows you how to create a property:
class Account(object):
def __init__(self):
self._acct_num = None
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Hello! Python by Anthony Briggs(9912)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9795)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8295)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7778)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7762)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Windows APT Warfare by Sheng-Hao Ma(6816)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6547)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6413)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6409)
Kotlin in Action by Dmitry Jemerov(5062)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4316)
Functional Programming in JavaScript by Mantyla Dan(4037)
Solidity Programming Essentials by Ritesh Modi(3991)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3782)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3723)
The Ultimate iOS Interview Playbook by Avi Tsadok(3699)
