UWPCE-PythonCert / PythonCertDevel

Development Repo for the Certificate Program
https://uwpce-pythoncert.github.io/PythonCertDevel/index.html
Other
7 stars 15 forks source link

adda bit about __class__ and subclassing to metaprogramming materials #178

Open PythonCHB opened 5 years ago

PythonCHB commented 5 years ago

In the Circle exercise, folks created methods that were not that friendly to subclassing.

use of the __class__ attribute could help with this:

In [61]: class C: 
    ...:     def test_name(self): 
    ...:         print("class name is:") 
    ...:         print(self.__class__.__name__) 
    ...:                                                                        

In [62]: c = C()                                                                

In [63]: c.test_name()                                                          
class name is:
C

similarly (and more importantly) for things like add, etc.

some notes and examples of this in the metaprogramming materials would be a good idea.