+ 1
object oriented programming #python
class Cat: def __init__(self, color, legs): self.color = color self.legs = legs # IS the following block is outside the class...?? what is the control flow....?? felix = Cat("ginger", 4) rover = Cat("dog-colored", 4) #class CAt is taking 3 arguements (formal) but 2 actual parameter.......?? stumpy = Cat("brown", 3)
3 Answers
+ 1
the self parameter is an instance of the class with which you are working (inside the class's methods). you cannot pass it there, it is automatically passed there by the interpreter
so color will be assigned ginger and legs will be 4
+ 1
This code defines a class named Cat, which has TWO ATTRIBUTES: COLOR and LEGS
Then the class is used to create 3 separate objects of that class.
WHERE ARE THESE 2 ATTRIBUTES DEFINED FOR THE CLASS SO THAT THEY CAN BE ACCESSED BY THE "SELF" PARAMETER......??
0
in python you don't explicitly define variables
you create attributes just by writing self.attr=something
there it is created
you can add to your constructor
self.age=10
and it will create new attribute