class Person:
def __init__(self, name, age):
self.name = name
self.age = age
class Student(Person):
def __init__(self, name, age, grade):
super().__init__(name, age)
self.grade = grade
Delegates Decorator
= Student("Alice", 12, 'A')
student print(student.name, student.age, student.grade)
Alice 12 A
= Student("Uma", 5.5, 'Good Kid!')
kid print(kid.name, kid.age, kid.grade)
Uma 5.5 Good Kid!
from fastcore.meta import delegates
class Person:
def __init__(self, name, age, **kwargs):
self.name = name
self.age = age
@delegates()
class Student(Person):
def __init__(self, name, age, grade, **kwargs):
super().__init__(name, age, **kwargs)
self.grade = grade
= Student("Amira", 6, grade='A', school='XYZ')
child child.__dict__
{'name': 'Amira', 'age': 6, 'grade': 'A'}
from fastcore.basics import GetAttr
class Strawberry(GetAttr):
def __init__(self, color, variety):
self.color, self.variety = color, variety
self.default = color
= Strawberry('red', 'Pegasus')
s s.color, s.variety, s.default
('red', 'Pegasus', 'red')
for s in dir(s) if not s.startswith('_')] [s
['capitalize',
'casefold',
'center',
'color',
'count',
'default',
'encode',
'endswith',
'expandtabs',
'find',
'format',
'format_map',
'index',
'isalnum',
'isalpha',
'isascii',
'isdecimal',
'isdigit',
'isidentifier',
'islower',
'isnumeric',
'isprintable',
'isspace',
'istitle',
'isupper',
'join',
'ljust',
'lower',
'lstrip',
'maketrans',
'partition',
'removeprefix',
'removesuffix',
'replace',
'rfind',
'rindex',
'rjust',
'rpartition',
'rsplit',
'rstrip',
'split',
'splitlines',
'startswith',
'strip',
'swapcase',
'title',
'translate',
'upper',
'variety',
'zfill']
def play(game, player='Daddy', num_players=2):
print(f'{player} is playing {game}')
'chess', 'Mehdiya') play(
Mehdiya is playing chess
@delegates(play)
def play_chess(player, speed='blitz', **kwargs):
'chess', player)
play(
'Uma') play_chess(
Uma is playing chess
print(play_chess.__signature__)
(player, speed='blitz', *, num_players=2)
import inspect
print(inspect.signature(play_chess))
(player, speed='blitz', *, num_players=2)
print(inspect.signature(play))
(game, player='Daddy', num_players=2)