Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #99724
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: variable vs. object |
| Date | 2015-11-30 19:19 +1100 |
| Message-ID | <mailman.22.1448871561.14615.python-list@python.org> (permalink) |
| References | <2b4696d5-c9fb-4ca6-92a3-564e47712d59@googlegroups.com> |
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.
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
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
csiph-web