-1

I'm stuck with the best way to create the following object:

I need a dictionary to be returned in the creation of the object. I could have used only one function, but the reality is that with the parameters that are passed in the creation of the object certain operations are performed.

This is the situation:

class Example(object): def __init__(self, param_a, param_b, param_c): self.a = param_a self.b = param_b self.c = param_c _tmp_1 = self._complex_function_1() _tmp_2 = self._complex_function_2() _tmp_3 = self._complex_function_3() # I need here to return a dictionary. Now I'm saving the # result in a variable call "self.values" and then access to # it from the object created, but I don't like this approach... def _complex_function_1(self): # Make some calculations using "self" def _complex_function_2(self): # Make some calculations using "self" def _complex_function_3(self): # Make some calculations using "self" 

And when I use it, I have to do the following:

e = Example(a, b, c) dict_values = e.values 

In short: What I want is for Example to return a dictionary. I do not think it's convenient to do it as separate functions because the methods are specific to Example to convert them into functions, and also because they use the parameters of self.

Any suggestions?

5
  • 4
    If you want to return a dictionary, why did you make the Example class? Just make a function that returns a dictionary. Commented Oct 29, 2018 at 12:23
  • @Aran-Fey as I said "I could have used only one function, but the reality is that with the parameters that are passed in the creation of the object certain operations are performed." Commented Oct 29, 2018 at 12:33
  • @nikolat328 I don't follow. Your class has no methods. It's trivial to rewrite it to a function. Commented Oct 29, 2018 at 12:34
  • @Aran-Fey my bad. I updated the question. Thanks for letting me know! Commented Oct 29, 2018 at 12:38
  • What would be e.values in your example? Commented Oct 29, 2018 at 13:34

1 Answer 1

2

If you really want to keep your Example class but only need the instance to compute values and create the result dict, you can just wrap the thing in a function:

def get_values(param_a, param_b, param_c): e = Example(param_a, param_b, param_c) return e.values() 

or make it a classmethod:

class Example(object): # ... @classmethod def get_values(cls, param_a, param_b, param_c): e = cls(param_a, param_b, param_c) return e.values() 

EDIT:

Is it better from the point of view of design patterns to perform the first example that you show or to separate all the methods in functions (which are specific to that class) and eliminate the class?

This actually has nothing to do with design patterns - there's no design pattern involved here anyway so asking which solution is "better from the point of view of design patterns" makes no sense.

From a design POV, there's no "one size fits all" answer either - it really depends on the concrete use case, object's responsability, needs for specialization / polymorphism etc. Also the answer here can only be valid for Python - the "best design for a given solution depends on the language too.

This being said, the first element here is whether you have (or can foresee in a near future) a need for specialization / polymorphism. If you know you have (or might soon) a need for specializing Example, you want to keep it as a class AND use the classmethod-based version of get_values().

Else, the next thing to consider is whether you really have a need for a class at all. If you have a complex state (tens of attributes etc) and some of the methods may change part of the state used by other methods, then using a class (well, an instance actually but this requires a class) can be more practical than passing all the necessary values from function to function.

If that's not the case - ie you only have three params to pass from function to function, no complex state to keep from one function call to another etc -, then using a class might be a bit overkill, and since Python doesn't require classes, you could indeed refactor this into plain functions - but is that worth the pain if you already have a working, tested implementation ?

EDIT #2

I was looking for the best way to write a JS code in Python.

Python is not JS.

This is how it looks. Basically it has a default function (export default function) and it uses a lot of "internal methods" to modify the funcion values, like force, apply, prepare). Do you know the best way to implement something like that in Python? github.com/d3/d3-force/blob/master/src/collide.js

This is a closure. Python has closures too, but idiomatic Python will use classes instead (as the old saying says: closures are the poor man's objects - and vice-versa) for anything a bit more involved than a decorator or simple callback.

From what I understand of this js snippet, the default collide function returns the force function, which has other functions as attributes, all of those functions working on the state stored in the closure. If that's correct, the Pythonic OO-based equivalent would be to make your example class callable - the js force function becoming Example.__call__, and other functions (apply etc) being implemented as plain methods.

Sign up to request clarification or add additional context in comments.

4 Comments

I like the first approach! I think I will use that, in which case I update my question: Is it better from the point of view of design patterns to perform the first example that you show or to separate all the methods in functions (which are specific to that class) and eliminate the class?
cf my edited answer.
I was looking for the best way to write a JS code in Python. This is how it looks. Basically it has a default function (export default function) and it uses a lot of "internal methods" to modify the funcion values, like force, apply, prepare). Do you know the best way to implement something like that in Python? github.com/d3/d3-force/blob/master/src/collide.js
cf my re-edited answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.