Skip to content

Instantly share code, notes, and snippets.

@UmerHA
Last active December 6, 2024 20:56
Show Gist options
  • Save UmerHA/f12c42146577df49bd8b18c5380a793e to your computer and use it in GitHub Desktop.
Save UmerHA/f12c42146577df49bd8b18c5380a793e to your computer and use it in GitHub Desktop.
Building a python class step by step via patching
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