Last active
December 6, 2024 20:56
-
-
Save UmerHA/f12c42146577df49bd8b18c5380a793e to your computer and use it in GitHub Desktop.
Building a python class step by step via patching
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
class Snail: | |
def __init__(self): | |
self.speed = 0.001 | |
self.pos = 0 | |
def move(self): | |
self.pos += self.speed | |
print(f'moved to {self.pos}') | |
garry = Snail() | |
garry.move() | |
garry.move() | |
# let's add the abilty to move fast to the Snail class | |
from fastcore.basics import patch | |
@patch | |
def move_fast(self:Snail): # the type hint Snail is important, that's how patch knows which class to add the method do | |
self.pos += (5 * self.speed) | |
print(f'moved to {self.pos}') | |
garry = Snail() | |
garry.move_fast() | |
garry.move_fast() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment