python 2.7 - What is the difference between these two examples? -
i going through python tutorial -
class pizza(object): @staticmethod def mix_ingredients(x, y): return x + y def cook(self): return self.mix_ingredients(self.cheese, self.vegetables) >>> pizza().cook pizza().cook false >>> pizza().mix_ingredients pizza.mix_ingredients true >>> pizza().mix_ingredients pizza().mix_ingredients true
i don't quite understand result of ---> pizza().cook pizza().cook - > why different?
and pizza().mix_ingredients pizza.mix_ingredients -> difference between having bracket , no bracket?
finally, static method, why final result true?
many thanks!
a way think see cook
method being 1 per pizza
, whereas pizza
s share same @staticmethod
mix_ingrecdients
.
so pizza()
gives new pizza, pizza().cook
cook method of specific pizza (indicated self
) so,
pizza().cook pizza().cook
will therefore false
.
this partially answers next part of question. pizza type , pizza() instance of type. when refer tho static method gives same function:
>>> pizza().mix_ingredients <function mix_ingredients @ 0x7f62a5eae668> >>> pizza.mix_ingredients <function mix_ingredients @ 0x7f62a5eae668>
if try instance method, "bound" , "unbound" functions.
Comments
Post a Comment