0

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)?

beoliver
  • 5,579
  • 5
  • 36
  • 72

4 Answers4

3

There is even a third one:

class MyThirdDict(dict):
    my_method = my_func

This behaves as if the function was defined in the class scope - every access to it leads to a call of the function's __get__, yielding a method.

Besides, in your 1st approach, I would do

class MyDict(dict):
    def my_method(self, a_tuple):
        return my_func(self, a_tuple)

in order to have the behaviour 100% identical.

From your 2 approaches, the 1st one is definitely more readable.

glglgl
  • 89,107
  • 13
  • 149
  • 217
  • would you recommend this third approach? It certainly seems the simplest! – beoliver Jun 19 '12 at 17:21
  • @ThemanontheClaphamomnibus Yes, I would. While it may not be very intuitive to the novice, it is closest to the intent - having methods, aka functions which take the special "self" parameter as first. The only downside is that this parameter isn't called "self" on the actual function, but as the function definition actually lies outside the class, I think it is ok. – glglgl Jun 19 '12 at 21:30
1

What about the simpler...

def my_func( a_dict, a_tuple ):
    a,b = a_tuple
    a_dict[a] = b
    return a_dict

class MyDict(dict):
    my_method = my_func
6502
  • 112,025
  • 15
  • 165
  • 265
1

An instance method is just a function that is an attribute of the class, so the following will work here:

class MyDict(dict):
    my_method = my_func

For example:

>>> def my_func( a_dict, a_tuple ):
...     a,b = a_tuple
...     a_dict[a] = b
...     return a_dict
... 
>>> class MyDict(dict):
...     my_method = my_func
... 
>>> d = MyDict()
>>> d.my_method(('a', 'b'))
{'a': 'b'}
>>> d
{'a': 'b'}

This looks a little confusing because self isn't used as the first parameter name, but it works the same way. The MyDict instance is passed in as the a_dict argument.

Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
0

The former would probably be considered cleaner - creating "methods" in your __init__ is kind of nasty (and also means that those methods aren't visible on the class definition).

Amber
  • 507,862
  • 82
  • 626
  • 550