Zero to Py by Michael Green
Author:Michael Green [Michael Green]
Language: eng
Format: epub
Publisher: leanpub.com
Published: 2023-02-20T00:00:00+00:00
Metaclasses can also be used to enforce from the library constraints or expectations on user code. Consider another example where a library expects users to define certain methods on a derived classes. You can use a metaclass to catch type errors at the class instantiation, instead of later during runtime.
1 >>> class LibraryMetaclass(type): 2 ... def __new__(cls, name, bases, attrs): 3 ... if "my_method" not in attrs: 4 ... raise AttributeError( 5 ... "derived class must define method my_method()" 6 ... ) 7 ... return type.__new__(cls, name, bases, attrs) 8 ... 9 >>> class BaseClass(metaclass=LibraryMetaclass): 10 ... my_method = None 11 ... 12 >>> class DerivedClass(BaseClass): 13 ... def my_method(self): 14 ... pass 15 ... 16 >>> class BadClass(BaseClass): 17 ... pass 18 Traceback (most recent call last): 19 File "<stdin>", line 1, in <module> 20 File "<stdin>", line 4, in __new__ 21 AttributeError: derived class must define method my_method()
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.
Business School Guides | GMAT |
Guides | Interviewing |
Job Hunting | Job Markets & Advice |
Resumes | Vocational Guidance |
Volunteer Work |
The Motivation Myth by Jeff Haden(4975)
Audition by Ryu Murakami(4594)
Adulting by Kelly Williams Brown(4208)
The Confidence Code by Katty Kay(4005)
Waiting in the Wings by Melissa Brayden(3107)
A Mind For Numbers: How to Excel at Math and Science (Even If You Flunked Algebra) by Barbara Oakley(3089)
Self-Esteem by Matthew McKay & Patrick Fanning(2932)
Nice Girls Don't Get the Corner Office by Lois P. Frankel(2918)
The ONE Thing by Gary Keller(2903)
Fooled by Randomness: The Hidden Role of Chance in Life and in the Markets by Nassim Nicholas Taleb(2840)
The Dictionary of Body Language by Joe Navarro(2803)
How to be More Interesting by Edward De Bono(2651)
Designing Your Life by Bill Burnett(2588)
Getting Things Done by David Allen(2570)
The Plant Paradox by Dr. Steven R. Gundry M.D(2404)
Police Exams Prep 2018-2019 by Kaplan Test Prep(2339)
What Color Is Your Parachute? 2015 by Richard N. Bolles(2196)
Dangerous Personalities by Joe Navarro(2166)
When to Jump by Mike Lewis(2043)
