Just a random file with a python tutor link:
Created
October 15, 2021 21:31
-
-
Save drin/0bc2364376ca701f5f18df9f97a10678 to your computer and use it in GitHub Desktop.
Random python example
This file contains 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 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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment