The definition of an object in Python

A

Avi Kak

Hello:

Please forgive me if my question is too silly or just not
well-formed.

Wesley Chun in his book (Core Python Programming) says that
**everything** in Python is an object. So I became curious
about the precise definition of an object in Python. My
curiosity was also driven by Wesley's statement that while
every class instance is an object, not every object is a
class instance.

Wesley says that every Python object must possess the following
three characteristics: 1) an identity, which can be retrieved
by the function id(); 2) a type, which can be retrieved by
the function type(); and 3) a value.

But when I do the following

mystring = 'hello'

print mystring.id()

print mystring.type()

Python complains that mystring does not possess the attributes
id and type.

So now I am confused. Any help with the resolution of this
issue would be much appreciated.

Avi Kak
(e-mail address removed)
 
A

Andrew Koenig

Avi> Wesley says that every Python object must possess the following
Avi> three characteristics: 1) an identity, which can be retrieved
Avi> by the function id(); 2) a type, which can be retrieved by
Avi> the function type(); and 3) a value.

Avi> But when I do the following

Avi> mystring = 'hello'

Avi> print mystring.id()

Avi> print mystring.type()

Avi> Python complains that mystring does not possess the attributes
Avi> id and type.

type and id are functions, not methods.
Traceback (most recent call last):
<type 'str'>
 
M

Michele Simionato

Andrew Koenig said:
Avi> Wesley says that every Python object must possess the following
Avi> three characteristics: 1) an identity, which can be retrieved
Avi> by the function id(); 2) a type, which can be retrieved by
Avi> the function type(); and 3) a value.

Avi> But when I do the following

Avi> mystring = 'hello'

Avi> print mystring.id()

Avi> print mystring.type()

Avi> Python complains that mystring does not possess the attributes
Avi> id and type.

type and id are functions, not methods.

Traceback (most recent call last):

<type 'str'>

Just for fun, to show that the built-ins "id" and "type" are regular objects:
135272896


Michele
 
W

Wesley J. Chun

Hello:

Please forgive me if my question is too silly or just not
well-formed.

you're forgiven. :) your question is well-formed *and* not silly.

Wesley Chun in his book (Core Python Programming) says that
**everything** in Python is an object. So I became curious
about the precise definition of an object in Python. My
curiosity was also driven by Wesley's statement that while
every class instance is an object,

this is still true since, as you know, *everything* is an object. :)

not every object is a class instance.

Python's treatment of objects is unique from other languages,
which makes this clause "true." Python has a defined set of
object types which are NOT classes, general objects such as
integers, floats, strings, lists, dictionaries, files, classes, and
even types are objects. a class instance is truly only an ob-
ject that has been instantiated from a user-defined class.

prior to Python 2.2, classes were "class" objects and instances
were "instance" objects. as types and classes are starting to
merge starting in 2.2, classes are now "type" objects, and
instance's "type" is the class they have been instantiated from.

EXAMPLE (old-style classes):
<type 'instance'>

EXAMPLE (new-style classes):
<type 'type'>

Wesley says that every Python object must possess the following
three characteristics: 1) an identity, which can be retrieved
by the function id(); 2) a type, which can be retrieved by
the function type(); and 3) a value.

again, this is also (still) true. (1) the identity is what makes an
object unique from every other object currently in the interpreter.
some people view this as a "memory address" altho you wouldn't
use it as such since Python handles the memory management 4 u.
you are just guaranteed that any other object in the system will
NOT have the same id. (2) the type of an object defines what its
characteristics (i.e., what methods and attributes it has, etc.) are,
and (3) the value goes without saying.

But when I do the following

mystring = 'hello'
print mystring.id()
print mystring.type()

Python complains that mystring does not possess the attributes
id and type.

So now I am confused. Any help with the resolution of this
issue would be much appreciated.

as others have pointed out, id() and type() are built-in FUNCTIONs
and not general built-in METHODs. each object type has its own
set of methods, and all behavior of an object and its attributes are
defined by its TYPE. you can use the dir() built-in function to see
what attributes (data and methods) an object has.

hope this helps!

-wesley

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

"Core Python Programming", Prentice Hall PTR, (c) 2001
http://starship.python.net/crew/wesc/cpp/

Silicon Valley-San Francisco Bay Area Python Users Group (BayPIGgies)
http://www.baypiggies.net

wesley.j.chun :: wesc at deirdre.org
cyberweb.consulting :: cyberweb_consulting at yahoo.com
http://roadkill.com/~wesc/cyberweb/

E-Mail using vi(m)/emacs with M$ outlook:
http://groups.google.com/[email protected]
 
B

Bengt Richter

On 19 Jul 2003 01:52:56 -0700, (e-mail address removed) (Wesley J. Chun) wrote:
[...]
again, this is also (still) true. (1) the identity is what makes an
object unique from every other object currently in the interpreter.
^^^^^^^^^
This should perhaps be emphasized. E.g., these results are not guaranteed,
but they can happen:
1

I.e., the value of id(x) is not valid beyond the lifetime of x, which is
obviously ok, but mistaken reuse of id-like numbers (e.g., such as you get from
file('somefile').fileno()) is a traditional source of bugs, and I imagine
naive use of id() could lead to similar ones.
some people view this as a "memory address" altho you wouldn't
use it as such since Python handles the memory management 4 u.
you are just guaranteed that any other object in the system will
NOT have the same id. (2) the type of an object defines what its
characteristics (i.e., what methods and attributes it has, etc.) are,
and (3) the value goes without saying.

Re (3): until, perhaps, you start taking about copying or pickling? ;-)

Regards,
Bengt Richter
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top