Skip to main content
typo...
Source Link
pillmuncher
  • 853
  • 1
  • 6
  • 7
def make_da_thing(maker, other, stuff):
    da_thing = maker(other + 1, stuff + 2)
    # ... do sth
    return da_thing

def maker_func(x, y):
     return x * y

class MakerClass(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y
...
a = make_da_thing(maker_func, 5, 8)
b = make_da_thing(MakerClass, 6, 7)
class On(object):
    is_on = True
    def switch(self):
        self.__class__ = Off

class Off(object):
    is_on = False
    def switch(self):
        self.__class__ = On
...

my_switch = On()
assert my_switch.is_on
my_switch.switch()
assert not my_switch.is_on
def make_da_thing(maker, other, stuff):
    da_thing = maker(other + 1, stuff + 2)
    # ... do sth
    return da_thing

def maker_func(x, y):
     return x * y

class MakerClass(object):
    def __init__(self, x, y)
        self.x = x
        self.y = y
...
a = make_da_thing(maker_func, 5, 8)
b = make_da_thing(MakerClass, 6, 7)
class On(object):
    is_on = True
    def switch(self):
        self.__class__ = Off

class Off(object):
    is_on = False
    def switch(self)
        self.__class__ = On
...

my_switch = On()
assert my_switch.is_on
my_switch.switch()
assert not my_switch.is_on
def make_da_thing(maker, other, stuff):
    da_thing = maker(other + 1, stuff + 2)
    # ... do sth
    return da_thing

def maker_func(x, y):
     return x * y

class MakerClass(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y
...
a = make_da_thing(maker_func, 5, 8)
b = make_da_thing(MakerClass, 6, 7)
class On(object):
    is_on = True
    def switch(self):
        self.__class__ = Off

class Off(object):
    is_on = False
    def switch(self):
        self.__class__ = On
...

my_switch = On()
assert my_switch.is_on
my_switch.switch()
assert not my_switch.is_on
typo...
Source Link
pillmuncher
  • 853
  • 1
  • 6
  • 7

Instead of setting up a Creational Pattern it's often enough to pass a callable around that creates objects. That might be a function, an object with a __call__ method or even a class, since there is no new() in Python, just an ivocationinvocation of the class itself:

Instead of setting up a Creational Pattern it's often enough to pass a callable around that creates objects. That might be a function, an object with a __call__ method or even a class, since there is no new() in Python, just an ivocation of the class itself:

Instead of setting up a Creational Pattern it's often enough to pass a callable around that creates objects. That might be a function, an object with a __call__ method or even a class, since there is no new() in Python, just an invocation of the class itself:

added 16 characters in body
Source Link
pillmuncher
  • 853
  • 1
  • 6
  • 7

Many situations that call for the application of a Pattern in a Static Language don't do so as much in Python. Many things can be solved with other thechniques, like higher order functions (decorators, function factories) or meta classes.

Many situations that call for the application of a Pattern in a Static Language don't do so as much in Python. Many things can be solved with other thechniques, like higher order functions (decorators, function factories).

Many situations that call for the application of a Pattern in a Static Language don't do so as much in Python. Many things can be solved with other thechniques, like higher order functions (decorators, function factories) or meta classes.

Source Link
pillmuncher
  • 853
  • 1
  • 6
  • 7
Loading