5/19 General question dot notation example


#1



https://www.codecademy.com/courses/python-intermediate-en-WL8e4/0/5?curriculum_id=4f89dab3d788890003000096


I completed the example, but I was curious why in the dot notation example they did not pass 'sides' to init, like we had to do for the example?


class Square(object):
  def __init__(self):
    self.sides = 4

my_shape = Square()
print my_shape.sides


#2

Because the class is called Square. It would be possible, but not logical to take sides as input to __init__, because a square always has 4 sides.


#3

I see, so you don't need to input all the variables that correspond to the attributes you want for a given function, only the ones that change?

for example, the exercise has you input 'name', but only b/c that changes

class Animal(object):
def init(self,name):
self.name=name
zebra=Animal('Jeffrey')
print zebra.name

but the example just has self.sides as a constant, so it's not listed in the input for the variable

class Square(object):
def init(self):
self.sides = 4

my_shape = Square()
print my_shape.sides

is this correct?


#4

It's a rule of thumb which will work in many cases. It's not a rule that's part of python, but just one that gives you a general idea.