Newbie question. Are those different objects ?

D

dec135

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.
 
R

random832

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 ?

y does not refer to the first object anymore after you've assigned the
second object to it. In CPython, if there are no other references to the
string object, yes it is deleted - other implementations may defer
deletion to a later time.
 
R

rusi

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.

You are also assuming that the two horizontal lines sometimes called 'equals'
have something to do with something called by the same name in math -- equations

Lets unassume that and rewrite the code

1. y ! raw_input('Enter a number:')
2. print type y
3. y ! float(raw_input('Enter a number:'))
4. print type y

Now read that 1 as first, 2 as second etc and read the '!' as 'MAKE'.
(It may help to shout it)

Now what was your question?
 
M

Mark Lawrence

You are also assuming that the two horizontal lines sometimes called 'equals'
have something to do with something called by the same name in math -- equations

A good point. Shall I write a PEP asking for a language change which
requires that that stupid = sign is replaced by a keyword reading
something like
thenameonthelefthandsideisassignedtheobjectontherighthandside ?
 
R

rusi

A good point. Shall I write a PEP asking for a language change which
requires that that stupid = sign is replaced by a keyword reading
something like
thenameonthelefthandsideisassignedtheobjectontherighthandside ?

Good idea. Only you were beaten to it by about 2 decades.

The language ABC calls it 'put' and corrects the unnecessary gratuitous
right to left order.
Reference
http://homepages.cwi.nl/~steven/abc/qr.html#COMMANDS

Examples
http://homepages.cwi.nl/~steven/abc/types.html

And what does that have to do with python?
http://www.onlamp.com/lpt/a/2431
 
M

Mark Lawrence

Good idea. Only you were beaten to it by about 2 decades.

I can't find a PEP suggesting this, can you give me the number please?
The language ABC calls it 'put' and corrects the unnecessary gratuitous
right to left order.

So does it go top to bottom or bottom to top? Or to really clarify
things does it have putlr, putrl, puttb and putbt?
 
8

88888 Dihedral

Well, in Python the assignment
operation = of a variable named y
in the LHS to the object of the RHS
result is more complicated
than = in those register basd
low level languages designed for
fast execution speeds in compiled
machine codes without an auto GC
bundled with the interpreter
in the run time.
 
R

rurpy

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).
 
T

Travis Griggs

A good point. Shall I write a PEP asking for a language change which requires that that stupid = sign is replaced by a keyword reading something like thenameonthelefthandsideisassignedtheobjectontherighthandside ?

Or a symbol like :=. As a former Smalltalker, I still miss this as the assignment operator, and the “gets” verbiage that went along with it. One said:

x := 4

as in “x gets 4”

I always got a kick out of the following paragraph from http://james-iry.blogspot.com/2009/05/brief-incomplete-and-mostly-wrong.html.

"1970 - Niklaus Wirth creates Pascal, a procedural language. Critics immediately denounce Pascal because it uses "x := x + y" syntax instead of the more familiar C-like "x = x + y". This criticism happens in spite of the fact that C has not yet been invented."
 
T

Terry Reedy

y = raw_input('Enter a number:')
print type y
y = float(raw_input('Enter a number:'))
print type y

I recommend starting with 3.3 unless your are forced to use 2.x.
I also recommend trying code before posting it.
I'm assuming that y is an object.

The name 'y' is bound to an object. The second assignment rebinds 'y' to
a different object.
I'm also assuming that the second and the first y are different objects

It depends on whether by 'y' you mean the name, which remains the same,
or the object it is bound to, which changes.
 
G

Gregory Ewing

rusi said:
Good idea. Only you were beaten to it by about 2 decades.

More than 2, I think.

Lisp: (setq x y)

Algol: x := y

Smalltalk: x <- y (where <- is a "left arrow" character)

Cobol: MOVE X TO Y
 
S

Steven D'Aprano

On 20/12/2013 15:34, rusi wrote:
A good point. Shall I write a PEP asking for a language change which
requires that that stupid = sign is replaced by a keyword reading
something like
thenameonthelefthandsideisassignedtheobjectontherighthandside ?

That's an excellent idea, and I look forward to reading the PEP.
 
D

Dennis Lee Bieber

That's an excellent idea, and I look forward to reading the PEP.

-1 vote... It still has the problematic "assigned" term...

I'd suggest:
thefullyqualifiednameontheleftafteranysubexpressionshavebeenevaluatedisattachedtotheobjectresultingfromevaluationoftheexpressionontheright

(heh, the spell-checker suggests that
"thefullyqualifiednameontheleftafteranysubexpressionshavebeenevaluatedisattachedt"
should be replaced with "textually")
 
C

Chris Angelico

(heh, the spell-checker suggests that
"thefullyqualifiednameontheleftafteranysubexpressionshavebeenevaluatedisattachedt"
should be replaced with "textually")

The spell-checker was scratching its head and going "I'm pretty sure
this isn't right, but I don't know enough of what it says to be sure
it's quite wrong!" and eventually gave up trying to understand you,
and just picked a word kinda like some of the letters and said "Here,
have this as a suggestion, I'm off to the pub. You don't pay me enough
to check words like that.".

ChrisA
 
G

Gene Heskett

The spell-checker was scratching its head and going "I'm pretty sure
this isn't right, but I don't know enough of what it says to be sure
it's quite wrong!" and eventually gave up trying to understand you,
and just picked a word kinda like some of the letters and said "Here,
have this as a suggestion, I'm off to the pub. You don't pay me enough
to check words like that.".

ChrisA

Lurking on this list is worth it just for the entertainment value, thanks
Chris. :)

Cheers, Gene
--
"There are four boxes to be used in defense of liberty:
soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)
Genes Web page <http://geneslinuxbox.net:6309/gene>

The heart is wiser than the intellect.
A pen in the hand of this president is far more
dangerous than 200 million guns in the hands of
law-abiding citizens.
 
M

Mark Lawrence

-1 vote... It still has the problematic "assigned" term...

I'd suggest:
thefullyqualifiednameontheleftafteranysubexpressionshavebeenevaluatedisattachedtotheobjectresultingfromevaluationoftheexpressionontheright

(heh, the spell-checker suggests that
"thefullyqualifiednameontheleftafteranysubexpressionshavebeenevaluatedisattachedt"
should be replaced with "textually")

Well you can write the PEP then :)
 
A

alex23

Shall I write a PEP asking for a language change which
requires that that stupid = sign is replaced by a keyword reading
something like
thenameonthelefthandsideisassignedtheobjectontherighthandside ?

I propose:

tag <obj> with <name>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top