How does the program know what actually '.sides' is?


#1

Capture


#2

Hi @textrockstar39356,

This statement, within the __init__ method of Square, assigns a value to the instance variable, sides, for all new instances of the Square type …

    self.sides = 4

#3

what is an ‘instance variable’ could you please elaborate?


#4

An instance variable is a variable for which there is a copy for each object of a particular type. For example, due to the manner in which you have defined Square, there will be a copy of the variable, sides, for each instance of Square that you create. In your code, you have created an instance of Square and assigned it to my_shape. Therefore, my_shape has its own copy of sides, which you are able to display, using print.

Since every square has four sides, you could have instead created a class variable by doing this prior to the __init__ method …

  sides = 4

Then a single copy of the sides variable would belong to the entire class.


#5

does instance mean object? and when did I create a variable named sides? it just said 'self.sides = 4’
but no any variable was created.


#6

Yes, an instance is an object. When we use the term instance we are usually making the point that we have an object of a particular class or type, for example an “instance of the Square class”. However, we can use the term in a more general sense, if we want.


#7

when did I create a variable named sides? it just said 'self.sides = 4’
but no any variable was created.


#8

When an instance of a class is created, the __init__ method for that class executes automatically. By placing this statement in the __init__ method for Square

    self.sides = 4

… you arranged for that statement to execute automatically whenever a new Square instance is created.

Then you created a new instance of Square with this statement …

my_shape = Square()

By including the variable, self, as the first parameter of a method, we are arranging for self to refer to the current instance of the class within that method. Therefore, as you assign the new instance of Square to my_square, that variable, self, is acting on behalf of my_square, and my_square winds up having its own copy of the variable, sides.


#9

I really didn’t understand what did you mean to say?


#10

Are you referring to the quoted phrase regarding no variable being created? That is from your post here.

This statement created the instance variable, sides

    self.sides = 4

… when the __init__ method executed automatically in response to this statement that created an instance of Square

my_shape = Square()

#11

I just want to ask in “self.sides = 4” how does the computer understands “sides” is a variable here as we hadn’t defined “sides” earlier.


#12

You are correct that we did not define sides earlier. This statement that you quoted defines that variable, as soon as it executes …

    self.sides = 4

Since, within the __init__ method, self refers to the new instance that we are creating and assigning to my_shape, the expression my_shape.sides refers to a valid defined variable by the time your print statement executes.


#13