Skip to content

Instantly share code, notes, and snippets.

@datavudeja
Forked from JacobCallahan/classtest.py
Created February 4, 2026 15:43
Show Gist options
  • Select an option

  • Save datavudeja/693c9cbbb98a10959535a91cbc77ef02 to your computer and use it in GitHub Desktop.

Select an option

Save datavudeja/693c9cbbb98a10959535a91cbc77ef02 to your computer and use it in GitHub Desktop.
Written during a quick class on python classes
import random
class MyBase:
"""This is a base class"""
def __init__(self, **kwargs):
"""Assign each key, value pair to our class instance"""
for arg, val in kwargs.items():
self.__dict__[arg] = val
def test_method(self):
"""Simply return 5"""
return 5
@staticmethod
def say_hello(name="World"):
"""A method that doesn't require a class instance"""
print(f"Hello, {name}!")
@property
def rando(self):
"""return a random key, value attribute pair"""
key = random.choice(list(self.__dict__.keys()))
return key, self.__dict__[key]
class Child(MyBase):
"""This is a child class"""
def __init__(self, name="Name", **kwargs):
"""Take in name, then call super for real init"""
self.name = name
super().__init__(**kwargs)
def test_method(self):
"""Override the parent class"""
return "Bob"
def do_something(self):
"""Call the original parent class' test_method"""
return super().test_method()
@classmethod
def fromstring(cls, in_str):
"""Use this class method to create a new instance from a sentence"""
arg_dict = {word: enum for enum, word in enumerate(in_str.split())}
return Child(**arg_dict)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment