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.