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


Groups > comp.lang.python > #99710 > unrolled thread

variable vs. object

Started byfl <rxjwg98@gmail.com>
First post2015-11-29 18:06 -0800
Last post2015-11-30 19:19 +1100
Articles 8 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  variable vs. object fl <rxjwg98@gmail.com> - 2015-11-29 18:06 -0800
    Re: variable vs. object Joel Goldstick <joel.goldstick@gmail.com> - 2015-11-29 21:16 -0500
    Re: variable vs. object André Roberge <andre.roberge@gmail.com> - 2015-11-29 18:24 -0800
      Re: variable vs. object Ben Finney <ben+python@benfinney.id.au> - 2015-11-30 13:45 +1100
    Re: variable vs. object Ben Finney <ben+python@benfinney.id.au> - 2015-11-30 13:41 +1100
    Re: variable vs. object Marko Rauhamaa <marko@pacujo.net> - 2015-11-30 07:28 +0200
    Re: variable vs. object Nagy László Zsolt <gandalf@shopzeus.com> - 2015-11-30 09:09 +0100
    Re: variable vs. object Chris Angelico <rosuav@gmail.com> - 2015-11-30 19:19 +1100

#99710 — variable vs. object

Fromfl <rxjwg98@gmail.com>
Date2015-11-29 18:06 -0800
Subjectvariable vs. object
Message-ID<2b4696d5-c9fb-4ca6-92a3-564e47712d59@googlegroups.com>
Hi,

I read several parts on line about Python that everything in Python is an 
object. Yes, it is a key difference with other languages. Then, I read a page
it says variables: global and local variable at:

http://www.tutorialspoint.com/python/python_functions.htm


I have a question that whether variables are objects?

For example,

a=10

'a' is an integer. Is it an object too?

Thanks,

[toc] | [next] | [standalone]


#99711

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2015-11-29 21:16 -0500
Message-ID<mailman.13.1448850192.14615.python-list@python.org>
In reply to#99710
On Sun, Nov 29, 2015 at 9:06 PM, fl <rxjwg98@gmail.com> wrote:

> Hi,
>
> I read several parts on line about Python that everything in Python is an
> object. Yes, it is a key difference with other languages. Then, I read a
> page
> it says variables: global and local variable at:
>
> http://www.tutorialspoint.com/python/python_functions.htm
>
>
> I have a question that whether variables are objects?
>
> For example,
>
> a=10
>
> 'a' is an integer. Is it an object too?
>

yes

>
> Thanks,
> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 
Joel Goldstick
http://joelgoldstick.com/stats/birthdays

[toc] | [prev] | [next] | [standalone]


#99712

FromAndré Roberge <andre.roberge@gmail.com>
Date2015-11-29 18:24 -0800
Message-ID<2c516888-de4f-4dd7-b1c0-fe56bb03f754@googlegroups.com>
In reply to#99710
On Sunday, 29 November 2015 22:06:58 UTC-4, fl  wrote:
> Hi,
> 
> I read several parts on line about Python that everything in Python is an 
> object. Yes, it is a key difference with other languages. Then, I read a page
> it says variables: global and local variable at:
> 
> http://www.tutorialspoint.com/python/python_functions.htm
> 
> 
> I have a question that whether variables are objects?
> 
> For example,
> 
> a=10
> 
> 'a' is an integer. Is it an object too?
> 
> Thanks,

In Python, a "variable" is a name given to an object.  In Python, the "=" sign is used to assign a name to an object: the name is on the left-hand side, and the object is on the right hand side.   Multiple names can be assigned to the same object.  In the example you gave, "a" is a name given to the object "10" which is an integer.    

If you do:

a = 10
b = a
a = "hello"

b will be 10.  b was just another name given to object 10 to which the name "a" was referring to at that point, even though we decided later that a should refer to the string "hello" (which is an object).

André

[toc] | [prev] | [next] | [standalone]


#99717

FromBen Finney <ben+python@benfinney.id.au>
Date2015-11-30 13:45 +1100
Message-ID<mailman.17.1448851821.14615.python-list@python.org>
In reply to#99712
André Roberge <andre.roberge@gmail.com> writes:

> In Python, a "variable" is a name given to an object. In Python, the
> "=" sign is used to assign a name to an object: the name is on the
> left-hand side, and the object is on the right hand side. Multiple
> names can be assigned to the same object.

Take care with the directionality of those statements.

In Python we don't give the name *to* the object, which would imply that
the object “has” that name in some sense. The object is totally
unaffected, and assignment does not give the object any knowledge about
that name.

We also don't assign names *to* objects; if anything, we assign the
object to the name.

It is the name that “has” the object. Or perhaps less confusingly, the
name is *bound to* the object.

It is frequently a point of confusion that assignment *never* affects
the object, so it's best to avoid giving that false impression.

> In the example you gave, "a" is a name given to the object "10" which
> is an integer.

Rather, I'd prefer to say that ‘a’ now refers to the object ‘10’. The
object ‘10’ was not “given” anything.

-- 
 \       “When I get new information, I change my position. What, sir, |
  `\             do you do with new information?” —John Maynard Keynes |
_o__)                                                                  |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#99716

FromBen Finney <ben+python@benfinney.id.au>
Date2015-11-30 13:41 +1100
Message-ID<mailman.16.1448851317.14615.python-list@python.org>
In reply to#99710
fl <rxjwg98@gmail.com> writes:

> I read several parts on line about Python that everything in Python is an 
> object.

Yes, every piece of information that you can get to with your program,
is made available as an object.

The phrase “everything is an object” is significant when newcomers are
surprised that, for example, every function is an object; every type is
an object; every number is an object.

> Then, I read a page it says variables […] I have a question that
> whether variables are objects?

A “variable”, as we use the term in Python, is a *way to access* a
specific object. It is a binding between a name and an object.

> For example,
>
> a=10
>
> 'a' is an integer. Is it an object too?

More specifically, ‘a’ is a name. That name is, at any point in time,
bound to some object. The object to which that name is bound is, in your
example, the integer ‘10’.

A variable is a specific kind of binding: a binding between one name and
one object. An assignment statement (‘a = 10’ in your example) binds a
reference to an object. If that reference is a name, then we call that a
“variable”.

The object is not a variable; objects typically do not know whether any
names are bound to them.

The binding from a name to an object is often called a variable.

Everyone serious about Python should watch Ned Batchelder's presentation
on names and values <URL:http://nedbatchelder.com/text/names.html>,
which covers this territory well.

-- 
 \       “I went to a general store. They wouldn't let me buy anything |
  `\                                     specifically.” —Steven Wright |
_o__)                                                                  |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#99721

FromMarko Rauhamaa <marko@pacujo.net>
Date2015-11-30 07:28 +0200
Message-ID<87y4dgys2h.fsf@elektro.pacujo.net>
In reply to#99710
fl <rxjwg98@gmail.com>:

> I read several parts on line about Python that everything in Python is
> an object.

Python has two distinct entities: objects and references.

All numbers, strings, classes, modules, class instances, files etc are
objects.

Variables, however, are not objects. They are references. Here are
different references:

   a                  # variable
   a.x                # attribute
   a[3]               # subscription

> Yes, it is a key difference with other languages.

Python shares this feature with many higher-level languages.


Marko

[toc] | [prev] | [next] | [standalone]


#99723

FromNagy László Zsolt <gandalf@shopzeus.com>
Date2015-11-30 09:09 +0100
Message-ID<mailman.21.1448870980.14615.python-list@python.org>
In reply to#99710
> a=10
>
> 'a' is an integer. Is it an object too?
In Python, objects have an identity. When you do "a=10" then you *bind*
the object to the name *a*. By "variable", the documentation refers to a
name that was bound to an object. This is different from many other low
level languages. For example: in C, you can do

a=10;

And it will "set the value of the variable 'a' to 10". In other words:
"a" is not just a name, it refers to a certain location in memory where
an integer value is stored. And in C, it is not an object but a value of
a built-in type.

In contrast, this is what happens in Python:

  * An "Integer" object is constructed with the value 10.
  * This object is then bound to the name "a".

The key point is that "variables" are just names that may reference to
objects. There is a distinction between names and objects. Strictly
speaking, you cannot "set the value of a variable", because variables do
not hold values. They just refer to objects. In Python, you can only
"create an object", or "change the state of an object", and "bind a name
to an object".

[toc] | [prev] | [next] | [standalone]


#99724

FromChris Angelico <rosuav@gmail.com>
Date2015-11-30 19:19 +1100
Message-ID<mailman.22.1448871561.14615.python-list@python.org>
In reply to#99710
On Mon, Nov 30, 2015 at 1:06 PM, fl <rxjwg98@gmail.com> wrote:
> For example,
>
> a=10
>
> 'a' is an integer. Is it an object too?

Other people have explained the difference between the name "a" and
the object it's bound to... but to the extent that "a" is an integer,
yes it most definitely is an object. To be specific, the integer 10 is
an object, as you can see thus:

>>> a = 10
>>> a.to_bytes(4, "big")
b'\x00\x00\x00\n'
>>> (a*12+8).to_bytes(4,"big")
b'\x00\x00\x00\x80'

Every expression [1] in Python has a value which is some sort of
object, so you can do method calls on anything at all. Sometimes this
looks a bit odd, but it does work:

>>> 7.25.as_integer_ratio()
(29, 4)

The floating-point number 7.25 is represented in Python as a float
object, and float objects have methods, same as all objects do. In
this case, I've asked Python to tell me what this number would be as a
fraction (ratio) of integers - 29/4 is equal to 7.25, so that's what
it returns.

So yes! It is an object. Everything is an object!

ChrisA

[1] To silence the nitpickers: An expression could raise an exception,
or not terminate at all. As Grandpa said in The Princess Bride, you're
very clever, now shut up.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web