0

Okay let me restructure this to be more theoretical - How do I separate a repetitive task inside a function.

Here is the scenario:

def some_class:
    def out_fxn():
        self.some_vars being used here
        for each value in some dict:
            if entry has type_A:
                get value, dict_index from entry
                some_function(my_dict, dict_key, value)
            if entry has type_B:
                get value, dict_index from entry
                some_function(my_dict, dict_key, value)
            if entry has type_C:
                get value, dict_index from entry
                some_function(my_dict, dict_key, value)
  1. Class Method? Should some_function(my_dict, dict_index, value) be a method of some_class? It will not use any class member variables and has not much to do with the class as such
  2. Nested Function? Should some_function(my_dict, dict_index, value) be inside out_fxn()? Nested Function?

Note: I could loop over ABC, assign values accordingly, but I just want to know if there is a way to do this using a function.

rtindru
  • 5,107
  • 9
  • 41
  • 59
  • There are some red flags here - dicts don't have indices, for one. And it's unlikely that you *have* to "run some_function()... on A, B, then C in order." That has a pretty funky code smell, but without knowing the specifics of what you're doing there's no way of telling you how to refactor it. – roippi Oct 08 '14 at 21:39
  • For literally no extra trouble you could have used syntacticly valid code in your for loop to be less ambiguous. Is `entry` the same as `each value`? Writing it as `for entry in some_dict.items():` would be less ambiguous. Then what is `value, dict_index` and how does it relate to entry? Are they properties, are they the same for each of the if cases or different? Is my_dict the same as "some dict"? – chthonicdaemon Oct 09 '14 at 04:54

2 Answers2

1
class some_class(object):
    def out_fxn(self):
        for entry in some_dict:
            for entry_type in ["type_A", "type_B", "type_C"]:
                entry_value, entry_key = self.get_entry_stuff(entry_type)
                self.some_fiunction(some_dict, entry_key, entry_value)

    def some_function(self, my_dict, dict_key, value):
        pass

    def get_entry_stuff(self, entry_type):
        if entry_type == "type_A":
            return "a value", "a key"
        elif entry_type == "type_B":
            return "b value", "b key"
        elif entry_type == "type_C":
            return "c value", "c key"
        else:
            raise ValueError("I don't know about type: %s" % repr(entry_type))
ErlVolton
  • 6,714
  • 2
  • 15
  • 26
  • So I make them class methods even though they are very specific to my function? – rtindru Oct 09 '14 at 04:40
  • 1
    A lot of python developers (myself included) put everything in classes. They give you added flexibility when in the future you decide you want to inherit some functionality. For creating groups of functions that are unbound, check out the classmethod and staticmethod decorators http://stackoverflow.com/questions/12179271/python-classmethod-and-staticmethod-for-beginner – ErlVolton Oct 09 '14 at 14:13
1

you can replace the if-else block with a loop:

for typei in [typeA, typeB, typeC]:
    if entry contains typei:
        run_function(entry)
user1526533
  • 441
  • 1
  • 5
  • 9