Created
August 31, 2021 14:44
-
-
Save dutc/bc76a67c65d3a006ecc0dbfb845cbb12 to your computer and use it in GitHub Desktop.
“Python Expert” Newsletter (Sep 1, 2021): Learning Corner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pandas import DataFrame | |
class Base: | |
def __len__(self): | |
return 1 | |
class Derived(Base): | |
def __len__(self): | |
return super().__len__() + 1 | |
class T: | |
def __init__(self, data): | |
self.data = data | |
def __len__(self): | |
return len(self.data) | |
if __name__ == '__main__': | |
df = DataFrame({ | |
'a': range(10) | |
}) | |
print( | |
df['a'], # __getitem__ | |
df.a, # __getattr__ | |
) |
For the full write-up and discussion, sign up for the “Python Expert” newsletter!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As you can see:
__
-method, a bytecode instruction, atp_
method—and these constitute a “protocol.”__getattr__