473,287 Members | 1,659 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,287 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 1371
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 :...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
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...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.