473,396 Members | 1,996 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,396 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 1377
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.