By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,715 Members | 748 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,715 IT Pros & Developers. It's quick & easy.

Re: Struct usages in Python

P: n/a
class Event(object):
>
Always subclass object, unless you have a very compelling reason not to,
or you are subclassing something else.
I've thought that if I write

class Event:
pass

, it'll be subclass of object too, I was wrong?

--
Best regards, Alex Gusarov
Jun 27 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Alex Gusarov schrieb:
> class Event(object):

Always subclass object, unless you have a very compelling reason not to,
or you are subclassing something else.

I've thought that if I write

class Event:
pass

, it'll be subclass of object too, I was wrong?

Yes. That is the somewhat unfortunate difference between new-style and
old-style classes. Use new-style if you can, and that means that
"object" must be part of the inheritance graph.

http://www.cafepy.com/article/python...d_objects.html

Diez
Jun 27 '08 #2

P: n/a
"Alex Gusarov" <al************@gmail.comwrites:
> class Event(object):

Always subclass object, unless you have a very compelling reason not to,
or you are subclassing something else.

I've thought that if I write

class Event:
pass

, it'll be subclass of object too, I was wrong?
You are wrong for Python 2.X, but right for Python 3 where old-style
classes are gone for good.

What you define with the statement

class Event: pass

is an 'old-style' class. Witness:
>>class Event: pass
...
>>class NewEvent(object): pass
...
>>type(Event)
<type 'classobj'>
>>type(NewEvent)
<type 'type'>
>>type(Event())
<type 'instance'>
del>>type(NewEvent())
<class '__main__.NewEvent'>

All old-style classes are actually objects of type 'classobj' (they
all have the same type!), all their instances are all of type 'instance'.
>>type(FooBar) == type(Event)
True
>>type(FooBar()) == type(Event())
True

Whereas instances of new-style classes are of type their class:
>>class NewFooBar(object): pass
...
>>type(NewFooBar) == type(NewEvent)
True
>>type(NewFooBar()) == type(NewEvent())
False

However, in python 2.X (X 2?), you can force all classes to of a
certain type by setting the global variable '__metaclass__'. So:
>>type(Event) # Event is an old-style class
<type 'classobj'>
>>__metaclass__ = type
class Event: pass
...
>>type(Event) # Now Event is new-style!
<type 'type'>

HTH

--
Arnaud
Jun 27 '08 #3

P: n/a
Arnaud Delobelle <ar*****@googlemail.comwrites:
"Alex Gusarov" <al************@gmail.comwrites:
>> class Event(object):

Always subclass object, unless you have a very compelling reason not to,
or you are subclassing something else.

I've thought that if I write

class Event:
pass

, it'll be subclass of object too, I was wrong?

You are wrong for Python 2.X, but right for Python 3 where old-style
classes are gone for good.

What you define with the statement

class Event: pass

is an 'old-style' class. Witness:
>>class Event: pass
...
>>class NewEvent(object): pass
...
>>type(Event)
<type 'classobj'>
>>type(NewEvent)
<type 'type'>
>>type(Event())
<type 'instance'>
del>>type(NewEvent())
<class '__main__.NewEvent'>

All old-style classes are actually objects of type 'classobj' (they
all have the same type!), all their instances are all of type 'instance'.
Oops somthing disappeared in the copy/paste process:
>>class FooBar: pass
...
>>type(FooBar) == type(Event)
True
>>type(FooBar()) == type(Event())
True

Whereas instances of new-style classes are of type their class:
>>class NewFooBar(object): pass
...
>>type(NewFooBar) == type(NewEvent)
True
>>type(NewFooBar()) == type(NewEvent())
False

However, in python 2.X (X 2?), you can force all classes to of a
certain type by setting the global variable '__metaclass__'. So:
>>type(Event) # Event is an old-style class
<type 'classobj'>
>>__metaclass__ = type
>>class Event: pass
...
>>type(Event) # Now Event is new-style!
<type 'type'>

HTH

--
Arnaud
Jun 27 '08 #4

P: n/a
Yes. That is the somewhat unfortunate difference between new-style and old-style classes.
Use new-style if you can, and that means that "object" must be part of the inheritance graph.
....
You are wrong for Python 2.X, but right for Python 3 where old-style

classes are gone for good.
Thanks, I don't knew it before and it's a sensitive information for me.

--
Best regards, Alex Gusarov
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.