473,326 Members | 2,147 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,326 software developers and data experts.

Re: Struct usages in Python

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
4 1373
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: andychambers2002 | last post by:
Hi All, A C library I'm using has a number of functions that all require a struct as an argument. The example module shows how to make a new Python Object from C code and I've seen other posts...
10
by: Giovanni Bajo | last post by:
Hello, given the ongoing work on struct (which I thought was a dead module), I was wondering if it would be possible to add an API to register custom parsing codes for struct. Whenever I use it...
0
by: Sudheer Gupta | last post by:
Hi, I am having trouble using C struct in python. Hope anyone can help me out ... Say, I have my C struct as typedef struct call { struct call *next;
3
by: Sudheer Gupta | last post by:
Hi, I am having trouble using C struct in python. Hope anyone can help me out ... Say, I have my C struct as typedef struct call { struct call *next;
4
by: Daniel Mark | last post by:
Hello all: I have found a useful module in IPython, named 'from IPython.ipstruct import Struct". So I can use it as follows: #################################### from IPython.ipstruct...
2
by: Pieter Rautenbach | last post by:
Hallo, I have a 64 bit server with CentOS 4.3 installed, running Python. $ uname -a Linux lutetium.mxit.co.za 2.6.9-34.ELsmp #1 SMP Thu Mar 9 06:23:23 GMT 2006 x86_64 x86_64 x86_64 GNU/Linux...
2
by: Jansson Christer | last post by:
Hi all, I have discovered that in my Python 2.4.1 installation (on Solaris 8), struct.pack handles things in a way that seems inconsistent to me. I haven't found any comprehensible...
4
by: Joshua J. Kugler | last post by:
I'm trying to put some values into a struct. Some of these values are NaN and Inf due to the nature of the data. As you well may know, struct (and other things) in Python <= 2.4 doesn't support...
5
by: emmanuel.rivoire | last post by:
Hello, I spent almost a week to be able to embed Python within my C++ game engine. I wrote a mini-tutorial of what I was able to do so far here :...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.