Skip to main content
added 854 characters in body
Source Link
One Lyner
  • 2k
  • 1
  • 8
  • 8

Abstract!

  • identical code should be a function;
  • similar code should be a function with a parameter;
  • unique code should be one function for each;
  • object oriented code can help here.

To get the idea:

def identical_code():
    lorem ipsum

def similar_code(parameter):
    lorem ipsum

class Base:
    def __call__(self, a, b, c):
        identical_code()
        similar_code(self.parameter())
        unique_code()
    def unique_code(self):
        NotImplemented
    def parameter(self):
        NotImplemented

class Func_1(Base):
    def unique_code(self):
        do_it_1()
    def parameter(self):
        return 1

class Func_2(Base):
    def unique_code(self):
        do_it_2()
    def parameter(self):
        return 2

You can then call Func1()(a, b, c) or Func_2()(a, b, c).

Here the NotImplemented methods are there to show that the base class is just there to define the common behavior, but can't really do anything if you don't specify those two methods.

Some maybe more pythonic way would be to just duck type and remove inheritance:

def func(a, b, c, setting):
    identical_code()
    similar_code(setting.parameter())
    setting.unique_code()

class Func_1:
    def unique_code(self):
        do_it_1()
    def parameter(self):
        return 1

class Func_2:
    def unique_code(self):
        do_it_2()
    def parameter(self):
        return 2

def func1(a, b, c):
    return func(a, b, c, Func_1)

def func2(a, b, c):
    return func(a, b, c, Func_2)

Abstract!

  • identical code should be a function;
  • similar code should be a function with a parameter;
  • unique code should be one function for each;
  • object oriented code can help here.

To get the idea:

def identical_code():
    lorem ipsum

def similar_code(parameter):
    lorem ipsum

class Base:
    def __call__(self, a, b, c):
        identical_code()
        similar_code(self.parameter())
        unique_code()
    def unique_code(self):
        NotImplemented
    def parameter(self):
        NotImplemented

class Func_1(Base):
    def unique_code(self):
        do_it_1()
    def parameter(self):
        return 1

class Func_2(Base):
    def unique_code(self):
        do_it_2()
    def parameter(self):
        return 2

Abstract!

  • identical code should be a function;
  • similar code should be a function with a parameter;
  • unique code should be one function for each;
  • object oriented code can help here.

To get the idea:

def identical_code():
    lorem ipsum

def similar_code(parameter):
    lorem ipsum

class Base:
    def __call__(self, a, b, c):
        identical_code()
        similar_code(self.parameter())
        unique_code()
    def unique_code(self):
        NotImplemented
    def parameter(self):
        NotImplemented

class Func_1(Base):
    def unique_code(self):
        do_it_1()
    def parameter(self):
        return 1

class Func_2(Base):
    def unique_code(self):
        do_it_2()
    def parameter(self):
        return 2

You can then call Func1()(a, b, c) or Func_2()(a, b, c).

Here the NotImplemented methods are there to show that the base class is just there to define the common behavior, but can't really do anything if you don't specify those two methods.

Some maybe more pythonic way would be to just duck type and remove inheritance:

def func(a, b, c, setting):
    identical_code()
    similar_code(setting.parameter())
    setting.unique_code()

class Func_1:
    def unique_code(self):
        do_it_1()
    def parameter(self):
        return 1

class Func_2:
    def unique_code(self):
        do_it_2()
    def parameter(self):
        return 2

def func1(a, b, c):
    return func(a, b, c, Func_1)

def func2(a, b, c):
    return func(a, b, c, Func_2)
Source Link
One Lyner
  • 2k
  • 1
  • 8
  • 8

Abstract!

  • identical code should be a function;
  • similar code should be a function with a parameter;
  • unique code should be one function for each;
  • object oriented code can help here.

To get the idea:

def identical_code():
    lorem ipsum

def similar_code(parameter):
    lorem ipsum

class Base:
    def __call__(self, a, b, c):
        identical_code()
        similar_code(self.parameter())
        unique_code()
    def unique_code(self):
        NotImplemented
    def parameter(self):
        NotImplemented

class Func_1(Base):
    def unique_code(self):
        do_it_1()
    def parameter(self):
        return 1

class Func_2(Base):
    def unique_code(self):
        do_it_2()
    def parameter(self):
        return 2