I have written a series of functions, but have been toying with the idea of using them to build a class. Instead of re-writing them all, I would like to call them from the class. What is the correct way of doing this? I can think of the following:
a boring function, but it gives the idea.
def my_func( a_dict, a_tuple ):
a,b = a_tuple
a_dict[a] = b
return a_dict
the two ways I had thought of were as follows:
class MyDict(dict):
def my_method(self, a_tuple):
return my_func(self, a_tuple)
or:
import functools
class MyOtherDict(dict):
def __init__(self):
self.my_parital = functools.partial(my_func, self)
it has been pointed out that the following will work as well. which seems the simplest!
class MySimpleDict(dict):
my_method = my_function
A Slight Extension of the question:
is it common to do this (call functions from methods)?