python - Why does this decorator calls "TypeError: make_sandwich() takes exactly 2 arguments (1 given)" -
so got around messing classes , decorators can't seem past error: typeerror: make_sandwich() takes 2 arguments (1 given)
i tried doing of outside of class , works. ideas on how make work inside sandwich maker?
class sandwichmaker: def __init__(self): self.sandwich = [] self.bread = "========" self.bacon = " ~~~~~~ " self.olives = " oooooo " def make_sandwich(self, insides): def add_bread(): self.sandwich.insert(0, self.bread) insides() self.sandwich.append(self.bread) return add_bread def add_bacon(self): return self.bacon def add_olives(self): return self.olives @make_sandwich def print_sandwich(self): in range(len(self.sandwich)): print self.sandwich[i] my_sandwich = sandwichmaker() my_sandwich.add_olives() my_sandwich.add_bacon() my_sandwich.print_sandwich() edit: hey, fixed answers. if wants grab working, nonsense sandwich printer, below final version:
def make_sandwich(insides): def add_bread(self): bread = "========" print bread insides(self) print bread return add_bread class sandwichmaker: def __init__(self): self.sandwich = [] self.bacon = " ~~~~~~ " self.olives = " oooooo " def add_bacon(self): self.sandwich.append(self.bacon) def add_olives(self): self.sandwich.append(self.olives) @make_sandwich def print_sandwich(self): in range(len(self.sandwich)): print self.sandwich[i] my_sandwich = sandwichmaker() my_sandwich.add_bacon() my_sandwich.add_olives() my_sandwich.add_bacon() my_sandwich.add_bacon() my_sandwich.print_sandwich()
@make_sandwich def print_sandwich(self): … when use decorator syntax that, function passed decorator. make_sandwich receives single argument. error says, takes two.
so need change make_sandwich take function reference, not implicit self sandwich instance.
as that, run 2 more issues: function returned make_sandwich not take argument, when call decorated print_sandwich, implicit self passed function not take argument. need change inner function take implcit self. last issue decorated function called without implicit self. need call insides(self) fix that.
the decorator should this:
def make_sandwich(insides): def add_bread(self): self.sandwich.insert(0, self.bread) insides(self) self.sandwich.append(self.bread) return add_bread
Comments
Post a Comment