Skip to content

Instantly share code, notes, and snippets.

@autocorr
Last active August 29, 2015 14:11
Show Gist options
  • Save autocorr/4f5568d853acd7024e04 to your computer and use it in GitHub Desktop.
Save autocorr/4f5568d853acd7024e04 to your computer and use it in GitHub Desktop.
Some OOP things
class BaseFitter(object):
# Put everything common to all fitting routines in this class
def __init__(self, *args):
# handle the arguments
pass
def interpolate(self):
pass
def calc_covariance(self):
pass
# You can use objects within objects to simplify things, this
# is called composition.
class FitOutputData(object):
def __init__(self):
# this class stores data like a structure
pass
def analysis(self):
# but a class can also have methods for analysis
# and other things
pass
# Inherit for different details in implementation
class FitOne(BaseFitter): # <- the inheritance part
def fit(self):
# fancy stuff
return FitOutputData(args) # <- the composition part
class FitTwo(BaseFitter):
# Same name `fit` means FitOne and FitTwo are called the same way
def fit(self):
pass
# At the end of the day, the code that you actually use can just be
# a simple function, and what you get is a class "FitOutputData":
def fit_func(xdata, args, Fitter=FitOne):
# call everything correctly and do things. the only requirement
# is that the fitter class passed has a fit method `.fit`.
# in the future this could even come from a different library
# or module, or if you shared the code the user could write their own!
fit = Fitter(...).fit()
return fit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment