Python Descriptors by Jacob Zimmerman
Author:Jacob Zimmerman
Language: eng
Format: epub
ISBN: 9781484237274
Publisher: Apress
def __get__(self, instance, owner):
return vars(instance)[self.name]
def __set__(self, instance, value):
vars(instance)[self.name] = value
def __delete__(self, instance):
del vars(instance)[self.name]
As shown, it is pretty easy to store on the instance. Some of you may not know about vars(), though, so I will explain. Calling vars() on an object returns the instance dictionary. Many of you probably knew about __dict__. The vars() function returns that same dictionary and is the preferred (read “Pythonic”) way of accessing it, though lesser known. It is preferred largely because of the lack of double underscores. Like nearly every other “magic” attribute with double underscores, there is a clean way of using it. Hopefully, now you will inform all of your Python-using buddies about this and it can become a much more widely known function.
But why should the values be accessed via vars() and not simple dot notation? There are actually plenty of situations where using dot notation would work just fine. In fact, it works in most situations. The only times there are problems is when the data descriptor has the same name that is being used for storage in the dictionary or if the name being used is not a legal Python identifier. Often, this case pops up because the descriptor is purposely storing the attribute under its own name, which is almost guaranteed to prevent name conflicts. But it’s still possible that an outside data descriptor has the same name as where the main descriptor is trying to store its data. In order to avoid this, it is preferable to always directly reference the instance’s dictionary. Another good reason is that it makes it more explicit and obvious where the data is being stored.
The next thing to be figured out is how the descriptor knows the name to store the attribute under. Hopefully it’s obvious that hard-coding a location is a bad idea; it prevents multiple instances of that type of descriptor from being used on the same class since they will all be contending for the same name.
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.
Coding Theory | Localization |
Logic | Object-Oriented Design |
Performance Optimization | Quality Control |
Reengineering | Robohelp |
Software Development | Software Reuse |
Structured Design | Testing |
Tools | UML |
Deep Learning with Python by François Chollet(12525)
Hello! Python by Anthony Briggs(9870)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9760)
The Mikado Method by Ola Ellnestam Daniel Brolund(9751)
Dependency Injection in .NET by Mark Seemann(9296)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8261)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7744)
Grails in Action by Glen Smith Peter Ledbrook(7670)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7520)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(6754)
Microservices with Go by Alexander Shuiskov(6521)
Practical Design Patterns for Java Developers by Miroslav Wengner(6419)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6397)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6382)
Angular Projects - Third Edition by Aristeidis Bampakos(5779)
The Art of Crafting User Stories by The Art of Crafting User Stories(5308)
NetSuite for Consultants - Second Edition by Peter Ries(5251)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5070)
Kotlin in Action by Dmitry Jemerov(5022)
