Could anyone explain __class__? 
Author Message
 Could anyone explain __class__?

I'm having trouble understanding the function of the built-in class method
__class__.  The documentation is terse, to say the least.

From code examples (UserList.py, forex.), it appears that self.__class__()
returns a new instance of the class of the instance in which it is called.
This would imply that the new instances' __init__ method would be called,
which would mean that you must make sure that you give __class__() the
correct arguments, just like you would if you instantiated the class in
the normal method (e.g. myInstance = UserList(<args>)).

Am I right, wrong, close, or what?



Wed, 05 Nov 2003 23:15:10 GMT  
 Could anyone explain __class__?

Quote:
> I'm having trouble understanding the function of the built-in class method
> __class__.  The documentation is terse, to say the least.

> From code examples (UserList.py, forex.), it appears that self.__class__()
> returns a new instance of the class of the instance in which it is called.
> This would imply that the new instances' __init__ method would be called,
> which would mean that you must make sure that you give __class__() the
> correct arguments, just like you would if you instantiated the class in
> the normal method (e.g. myInstance = UserList(<args>)).

> Am I right, wrong, close, or what?

    Seems to be a reference to the class object.

Quote:
>>> class Parent:

...     pass
...
Quote:
>>> parent = Parent()
>>> dir(parent)
[]
>>> parent.__class__

<class __main__.Parent at 0x8056b9c>
Quote:
>>> class Parent:

...     pass
...
Quote:
>>> parent = Parent()
>>> dir(parent)
[]
>>> parent.__class__

<class __main__.Parent at 0x804dafc>

    So here, parent.__class__ is the same as Parent, so calling it as a
function would return a new instance of that class.

    BTW, does anyone know why dir(parent) did not show __class__ as one of the
attributes? Does it not show inherited attributes?

    Cheers,

    Mike

--

"With sufficient thrust, pigs fly just fine. However, this is not necessarily a
good idea. It is hard to be sure where they are going to land, and it could be
dangerous sitting under them as they fly overhead." -- RFC 1925



Wed, 05 Nov 2003 23:30:18 GMT  
 Could anyone explain __class__?

Quote:
> I'm having trouble understanding the function of the built-in class
> method __class__.  The documentation is terse, to say the least.

> From code examples (UserList.py, forex.), it appears that
> self.__class__() returns a new instance of the class of the instance
> in which it is called.

The idea with UserList is that it's to be inherited from.  See
http://www.python.org/doc/current/tut/node11.html#SECTION001150000000...
for a brief explanation.  UserList instantiates return values using the
class referred to by the self.__class__ attribute because in most cases
it's expected that adding two instances of a class will return another
instance of that class, and self.__class__ is just the class of the
instance calling the current method.  Here's a simple example:

from UserList import UserList

class MyList(UserList):

    '''A subclass of UserList with a frivolous attribute.'''

    def __init__(self, initlist=None):
        UserList.__init__(self, initlist)
        self.my_frivolous_attribute = None

l = MyList([1])
m = MyList([2])

new_mylist = l+m
assert (new_mylist.__class__ == MyList) and \
       hasattr(new_mylist, 'my_frivolous_attribute')

HTH
Alex.



Wed, 05 Nov 2003 23:34:35 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Can anyone explain this.....

2. Anyone explain PROP:Touched?

3. Can anyone explain what attribute grammars are?

4. Can anyone explain the following code ?

5. Can anyone explain why..

6. Can anyone explain : Synthesis, verification,..?

7. Can anyone explain ?

8. Can anyone explain this?

9. Can anyone explain this behaviour?

10. Is anyone capable of explaining ?

11. Please Explain: Class Methods

12. Want docs explaining Window classes in VW

 

 
Powered by phpBB® Forum Software