6.00 2nd lesson: the professor writes : y = raw_input('Enter a number:') print type y y = float(raw_input('Enter a number:')) print type y I'm assuming that y is an object. I'm also assuming that the second and the first y are different objects because they have different types. The second time we type print type y, how does the program knows which one of the y's it refers to ? Is the first y object deleted ? thanks in advance.
I may be wrong. It is the same object, but it changes. So, first y is the number we put (example: 6), and it will print y, then it will convert the number you put into a float.
Give me a sec and you will understand better
Is this java?
no it is python. Previous to write that on the board, the teacher explained that an object has only one type. Here there are two types, that's why I'm thinking those are two different objects. Are you saying that the object is changing type ? Is that possible ?
I've asked my question on a python forum and it seems like it is deleted.
It's the same variable y, it's just that the value is being reassigned (assuming you enter an int or a bool for y) assuming you enter an integer like 4 >>y = raw_input('Enter a number: ') #gets input from user that will be stored in y Enter a number: 4 #stores the value 4 in the variable y >>print type(y) <type 'int'> #because 4 is an object of type 'int' >>y = float(raw_input('Enter a number: ') #gets new input for y changes it to float Enter a number: 4 #this is equivalent to y=float(4) >>print type(y) <type 'float'> #because the int 4 got converted by to a float
I've go a good response i'll publish it here : Rather than thinking that y "is" an object, it is more accurate to think of it as: y is a name that is "bound" to (ie, refers to, points to) an object. So, raw_input() creates a string object and returns it. Your first assignment statement binds that string object to the name "y". From now on, when you refer to "y" you will get that string object. When python executes your 3rd line, raw_input() creates a new string object, completely separate from the earlier one. This object is passed to float(). Float() reads it and creates a new float object and returns it. When python then executes your second assignment statement, it changes the binding of "y" to point to the float object; the old binding to the string object is lost. From now on, when you refer to "y" you will get the float object.
Join our real-time social learning platform and learn together with your friends!