Skip to content

Instantly share code, notes, and snippets.

@drin
Created October 15, 2021 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drin/0bc2364376ca701f5f18df9f97a10678 to your computer and use it in GitHub Desktop.
Save drin/0bc2364376ca701f5f18df9f97a10678 to your computer and use it in GitHub Desktop.
Random python example
class ExampleClass:
class_var = 'Class Variable'
def __init__(self, req_param, def_param=10, **kwargs):
# calling super class "constructor" is *optional*
super().__init__()
self.required_arg = req_param
self.optional_arg = def_param
# The `**` prefix means kwargs is a dictionary
# but it comes from named arguments (e.g. name=val)
# `dict` is the type for a dictionary, invoking it
# on `kwargs` should essentially make a deep copy
self.extra_args = dict(kwargs)
def __str__(self):
"""
This is a docstring, documenting this function.
The `__str__` function is called by convention when
passing an instance of this class to the `str` function.
"""
# using format strings, because they're cool
# adjacent strings are automatically concatenated
return (
f'Value from required arg: {self.required_arg}\n'
f'Value from arg with default val: {self.optional_arg}\n'
f'Received kwargs (keyword args):\n\t{self.extra_args}\n'
)
# Now we just use the class as an example
test_obj_one = ExampleClass('obj one')
print(str(test_obj_one))
test_obj_two = ExampleClass('obj two', kw_param_one='kw val')
print(str(test_obj_two))

Just a random file with a python tutor link:

https://pythontutor.com/visualize.html#code=class%20ExampleClass%3A%0A%20%20%20%20class_var%20%3D%20'Class%20Variable'%0A%20%20%20%20%0A%20%20%20%20def%20__init__%28self,%20req_param,%20def_param%3D10,%20**kwargs%29%3A%0A%20%20%20%20%20%20%20%20%23%20calling%20super%20class%20%22constructor%22%20is%20*optional*%0A%20%20%20%20%20%20%20%20super%28%29.__init__%28%29%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20self.required_arg%20%3D%20req_param%0A%20%20%20%20%20%20%20%20self.optional_arg%20%3D%20def_param%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%23%20The%20%60**%60%20prefix%20means%20kwargs%20is%20a%20dictionary%0A%20%20%20%20%20%20%20%20%23%20but%20it%20comes%20from%20named%20arguments%20%28e.g.%20name%3Dval%29%0A%20%20%20%20%20%20%20%20%23%20%60dict%60%20is%20the%20type%20for%20a%20dictionary,%20invoking%20it%0A%20%20%20%20%20%20%20%20%23%20on%20%60kwargs%60%20should%20essentially%20make%20a%20deep%20copy%0A%20%20%20%20%20%20%20%20self.extra_args%20%20%20%3D%20dict%28kwargs%29%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20def%20__str__%28self%29%3A%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20This%20is%20a%20docstring,%20documenting%20this%20function.%0A%20%20%20%20%20%20%20%20The%20%60__str__%60%20function%20is%20called%20by%20convention%20when%0A%20%20%20%20%20%20%20%20passing%20an%20instance%20of%20this%20class%20to%20the%20%60str%60%20function.%0A%20%20%20%20%20%20%20%20%22%22%22%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%23%20using%20format%20strings,%20because%20they're%20cool%0A%20%20%20%20%20%20%20%20%23%20adjacent%20strings%20are%20automatically%20concatenated%0A%20%20%20%20%20%20%20%20return%20%28%0A%20%20%20%20%20%20%20%20%20%20%20%20f'Value%20from%20required%20arg%3A%20%7Bself.required_arg%7D%5Cn'%0A%20%20%20%20%20%20%20%20%20%20%20%20f'Value%20from%20arg%20with%20default%20val%3A%20%7Bself.optional_arg%7D%5Cn'%0A%20%20%20%20%20%20%20%20%20%20%20%20f'Received%20kwargs%20%28keyword%20args%29%3A%5Cn%5Ct%7Bself.extra_args%7D%5Cn'%0A%20%20%20%20%20%20%20%20%29%0A%0A%23%20Now%20we%20just%20use%20the%20class%20as%20an%20example%0Atest_obj_one%20%3D%20ExampleClass%28'obj%20one'%29%0Aprint%28str%28test_obj_one%29%29%0A%0Atest_obj_two%20%3D%20ExampleClass%28'obj%20two',%20kw_param_one%3D'kw%20val'%29%0Aprint%28str%28test_obj_two%29%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment