473,405 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

The definition of an object in Python

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
ka*@purdue.edu
Jul 18 '05 #1
4 4745
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.
mystring = 'hello'
print mystring.id() # using id as a method doesn't work Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'str' object has no attribute 'id' print id(mystring) # using id as a function does 1456272 print type(mystring) # ditto for type

<type 'str'>
--
Andrew Koenig, ar*@acm.org
Jul 18 '05 #2
Andrew Koenig <ar*@acm.org> wrote in message news:<yu**************@tinker.research.att.com>...
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.
mystring = 'hello'
print mystring.id() # using id as a method doesn't work Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'str' object has no attribute 'id' print id(mystring) # using id as a function does 1456272 print type(mystring) # ditto for type <type 'str'>


Just for fun, to show that the built-ins "id" and "type" are regular objects:
id(id) 1076547724 type(type) <type 'type'> type(id) <type 'builtin_function_or_method'> id(type)

135272896
Michele
Jul 18 '05 #3
ka*@purdue.edu (Avi Kak) wrote in message news:<94**************************@posting.google. com>...
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):
class C: pass .... c=C()
type(C) <type 'class'> type(c) <type 'instance'>

EXAMPLE (new-style classes):
class myInt(int): pass .... i = myInt()
type(i) <class '__main__.myInt'> type(myInt)

<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/groups?thre...ing.google.com
Jul 18 '05 #4
On 19 Jul 2003 01:52:56 -0700, we**@deirdre.org (Wesley J. Chun) wrote:
[...]
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.

^^^^^^^^^
This should perhaps be emphasized. E.g., these results are not guaranteed,
but they can happen:
idvalue = id(123)
idvalue, id(456) (7952152, 7952152) idvalue == id(456)

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
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tito | last post by:
From the Python's tutorial, about default argument values: <quote> The default value is evaluated only once. This makes a difference when the default is a mutable object such as a list,...
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
8
by: Sylvain Ferriol | last post by:
hello everybody, i want to talk with you about a question i have in mind and i do not find a answer. it 's simple: why do we not have a beatiful syntax for object definition as we have for...
3
by: Yves Dorfsman | last post by:
Does it make a difference if you put subclass object or not ? What is the difference between c1 and c2 here: class c1: pass class c2(object): pass
27
by: notnorwegian | last post by:
(might not be the right forum for this but...) what is the definition of a highlevel-language? well there isnt one specifically and wikipedia and the like gives just a very general description...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.