Last active
February 26, 2025 19:08
-
-
Save slayerlab/7eeaf913fa1c3266c9681a07177d7185 to your computer and use it in GitHub Desktop.
[PoC] Modify an instance method using the built-in class type.
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
#!/usr/bin/env python3 | |
# The focus here is the L#34 and L#35. | |
class Birthday: | |
def __init__(self, day=1, month=2, year=3): | |
self._day = day | |
self._month = month | |
self._year = year | |
def show(self): | |
return f"Day: {self._day}, Month: {self._month}, Year: {self._year}" | |
class Person: | |
def __init__(self, first='tom', last='angelripper', birthday=None): | |
self._first = first | |
self._last = last | |
if birthday is None: | |
birthday = Birthday() | |
self._birthday = birthday | |
@property | |
def name(self): | |
return f"{self._first} {self._last}\n" + self._birthday.show() | |
def _(*args): | |
retval = s._birthday._show() | |
return retval.replace(',', '/') | |
s = Person() | |
print('Before:',s._birthday.show()) | |
s._birthday._show = s._birthday.show | |
s._birthday.show = type(s._birthday.show)(_, ()) | |
print('After:',s._birthday.show()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: