python - What is a good way to implement several very similar functions? -
i need several similar plotting functions in python share many arguments, differ in , of course differ in do. came far:
- obviously defining them 1 after other , copying code share possibility, though not one, reckon.
- one transfer "shared" part of code helper functions , call these inside different plotting functions. make tedious though, later add features functions should have.
- and i've thought of implementing 1 "big" function, making possibly not needed arguments optional , deciding on in function body based on additional arguments. this, believe, make difficult though, find out happens in specific case 1 face forest of arguments.
i can rule out first option, i'm hard pressed decide between second , third. started wondering: there another, maybe object-oriented, way? , if not, how 1 decide between option 2 , three?
i hope question not general , guess not python-specific, since rather new programming (i've never done oop) , first thought now, guess add python tag.
edit:
as pointed out many, question quite general , intended so, understand makes answering rather difficult. here's info on problem caused me ask:
i need plot simulation data, plotting problems have simulation parameters in common (location of files, physical parameters,...). want figure design same. depending on quantity, plots 1d, 2d, should contain more 1 figure, need normalize data or take logarithm before plotting it. output format might vary.
i hope helps bit.
how this. can create base class have method foo
base shared method performs similar code. different classes can inherit base
, super method of interest , extend implementation whatever functionality need.
here example of how works. note different example provided between how use super in python 2 , python 3.
class base: def foo(self, *args, **kwargs): print("foo stuff base") return "return here" class someclass(base): def foo(self, *args, **kwargs): # python 2 #x = super(someclass, self).foo(*args, **kwargs) # python 3 x = super().foo(*args, **kwargs) print(x) print("someclass extension of foo") s = someclass() s.foo()
output:
foo stuff base return here someclass extension of foo base
Comments
Post a Comment