Re: variable vs. object

2015-11-30 Thread Nagy László Zsolt

> 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".
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: variable vs. object

2015-11-30 Thread Chris Angelico
On Mon, Nov 30, 2015 at 1:06 PM, fl  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.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: variable vs. object

2015-11-29 Thread Ben Finney
André Roberge  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

-- 
https://mail.python.org/mailman/listinfo/python-list


variable vs. object

2015-11-29 Thread fl
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,
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: variable vs. object

2015-11-29 Thread Marko Rauhamaa
fl :

> 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
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: variable vs. object

2015-11-29 Thread Joel Goldstick
On Sun, Nov 29, 2015 at 9:06 PM, 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?
>

yes

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



-- 
Joel Goldstick
http://joelgoldstick.com/stats/birthdays
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: variable vs. object

2015-11-29 Thread André Roberge
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é
-- 
https://mail.python.org/mailman/listinfo/python-list