[Tutor] what is an object?

Gregor Lingl glingl at aon.at
Fri Feb 27 19:23:52 EST 2004



Christopher Spears schrieb:

>  I have a very
>fundamental question to ask.  What exactly is an
>object?  A part of my understands what an object is,
>but I cannot articulate this to another person.
>
>  
>
Hmm, the most fundamental questions  tend to be the most difficult
ones to answer. So I'm certainly not the right person for this and were
it only because of  my limited ability to express my thoughts in proper
English.

Nevertheless I'll try to give you a few (unordered) remarks. There are
certainly others on the list, who will give you more complete and more
"exact" answers.

(0) I assume you want to know what an object in Python is - not in real
life (ore in some other computer language - the concepts differ ...)

(1) In Python (nearly) everything is an object.

or

(1') An object is the representation of some*thing* you can
think of, or you can find in the real world, in your computer.

(2) examples:
3
"huu"
True
[1,2,3]
{}
len

What means nearly? The following is *not* an object
*but* a statement:

class A:
    def __init__(self, name):
        self.name=name
    def whoareyou(self):
        print self.name

But if this statement is executed by the Python interpreter you have

- the possibility to produce new objects:

obj1 = A("Frank")
obj2 = A("Jude")

now obj1 and obj2 are objects.

- and you've got another new object:

A

sorry, this is a class-object. (I'm anxious that we now are
at a point, where further explanation  will  make things
make more unclear than clear - and that beeing content
with a more superficial explanation ist the right thing here ...)

If you think of something to be an object, you always
can use the built-in function type to determine which
type  (sort) of object it is:

 >>> type(True)
<type 'bool'>
 >>> type(3)
<type 'int'>
 >>> type("huu")
<type 'str'>
 >>> type(True)
<type 'bool'>
 >>> type([1,2,3])
<type 'list'>
 >>> type({})
<type 'dict'>
 >>> type(len)
<type 'builtin_function_or_method'>
 >>> class A:
    def __init__(self,name):
        self.name=name
    def whoareyou(self):
        print self.name

       
 >>> obj1 = A("Frank")
 >>> obj2 = A("Jude")
 >>> type(obj1)
<type 'instance'>
 >>> type(A)
<type 'classobj'>
 >>>

One disitinction you should observe is the difference
between built-in objects - "types" - an user defined objects
produced by a user defined class.

Since nearly everything is an object, it'Äs hard to chracterize
objects because there are so much differen flavours.

Perhaps what is important to note is:

many of them can take part on operations like +, *,  etc....
or as you saw  operations with [] like a[0] ....

Many of them have attributes.
(a) instance attributes (which are not "callable")
 >>> obj1.name
'Frank'
(b) and methods, which represent the ability of objects to perform some 
tasks
and are essentially functions bound to their objects ...
 >>> obj2.whoareyou()
Jude
 >>> "hoo gaz gar".split()
['hoo', 'gaz', 'gar']

Think about this now, read more answers certainly to come
from others and  think about them for a few days. Then
determine what you want to clarify next and come back
with your next "objects-centered" question.

Regards,
Gregor

>-Chris
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>
>  
>



More information about the Tutor mailing list