Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #62449

Re: Newbie question. Are those different objects ?

Newsgroups comp.lang.python
Date 2013-12-20 10:06 -0800
References <af844bcf-ed55-42da-8a9f-b8270dcc4028@googlegroups.com>
Message-ID <a9b4dfa4-7941-4371-ba8c-c4700a3d4dda@googlegroups.com> (permalink)
Subject Re: Newbie question. Are those different objects ?
From rurpy@yahoo.com

Show all headers | View raw


On 12/20/2013 08:16 AM, dec135@msn.com wrote:
> 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.

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.

> I'm also assuming that the second and the first y are different
> objects because they have different types. 

Yes, they are different objects.  But not because they have 
different types; they are different because every time python
creates a new object it is distinct from other objects [*1].

> The second time we type
> print type y, how does the program knows which one of the y's it
> refers to ?  

Because there is only one name "y", and when python executed
your second assignment statement, it changed the object that
the name y pointed to from the first (string) object to the 
second (float) one.

> Is the first y object deleted ? thanks in advance.

Yes.  If there is no way that the first object can be accessed 
any more, then it will be deleted.  The same thing happened to 
the string object return by raw_input() in your 3rd statement
(which never had a name at all).

----
[*1] My statement was an oversimplification.  There are some 
cases where Python will return the same object such as interned
objects and objects like None for which there is only ever a 
single instance in a Python program.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Newbie question. Are those different objects ? dec135@msn.com - 2013-12-20 07:16 -0800
  Re: Newbie question. Are those different objects ? random832@fastmail.us - 2013-12-20 10:24 -0500
  Re: Newbie question. Are those different objects ? rusi <rustompmody@gmail.com> - 2013-12-20 07:34 -0800
    Re: Newbie question. Are those different objects ? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-20 16:00 +0000
      Re: Newbie question. Are those different objects ? rusi <rustompmody@gmail.com> - 2013-12-20 09:10 -0800
        Re: Newbie question. Are those different objects ? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-20 17:29 +0000
        Re: Newbie question. Are those different objects ? 88888 Dihedral <dihedral88888@gmail.com> - 2013-12-20 09:59 -0800
        Re: Newbie question. Are those different objects ? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2013-12-21 10:56 +1300
          Re: Newbie question. Are those different objects ? Duncan Booth <duncan.booth@invalid.invalid> - 2013-12-23 08:19 +0000
      Re: Newbie question. Are those different objects ? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-12-21 12:58 +0000
        Re: Newbie question. Are those different objects ? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-12-21 11:54 -0500
        Re: Newbie question. Are those different objects ? Chris Angelico <rosuav@gmail.com> - 2013-12-22 05:39 +1100
        Re: Newbie question. Are those different objects ? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-21 19:17 +0000
      Re: Newbie question. Are those different objects ? alex23 <wuwei23@gmail.com> - 2013-12-23 11:31 +1000
    Re: Newbie question. Are those different objects ? Travis Griggs <travisgriggs@gmail.com> - 2013-12-20 10:24 -0800
  Re: Newbie question. Are those different objects ? bob gailer <bgailer@gmail.com> - 2013-12-20 12:02 -0500
  Re: Newbie question. Are those different objects ? rurpy@yahoo.com - 2013-12-20 10:06 -0800
  Re: Newbie question. Are those different objects ? Terry Reedy <tjreedy@udel.edu> - 2013-12-20 16:56 -0500
  Re: Newbie question. Are those different objects ? Gene Heskett <gheskett@wdtv.com> - 2013-12-21 14:08 -0500

csiph-web