473,385 Members | 1,769 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,385 software developers and data experts.

New vs Old Style Python Classes in C Extensions?

While I have a reasonable understanding of the differences in new-style versus
old-style classes, tonight while working a C extension module I realized I
don't know how to indicate which style my C extension module should appear as.

I'm following the Python docs for extended modules, but it doesn't say in
there anyplace I can find which style I'm creating. My clue that something
was wrong is when this:

from cextension import Context

class MyContext(Context):

def __init__(self):
super(Context, self).__init__()

repeatedly and reliably failed with a corrupted C data structure, while this:

class MyContext(Context):

def __init__(self):
Context.__init__()

worked without any problems. As I understand it, the former uses new-style
semantics while the latter uses old-style, and -thats- when I realized I have
no idea which my C extension implemented.

Any enlightenment?

-Jeff

Jan 27 '07 #1
2 1760
Jeff Rush schrieb:
from cextension import Context

class MyContext(Context):

def __init__(self):
super(Context, self).__init__()

repeatedly and reliably failed with a corrupted C data structure, while
this:

class MyContext(Context):

def __init__(self):
Context.__init__()

worked without any problems. As I understand it, the former uses
new-style semantics while the latter uses old-style,
This understanding is incorrect: Context is either a new-style
or an old-style class (if type(Context) is type, it's a new-style
class, if type(context) is named 'classobj', it's an old-style
class).

As you have implemented Context in an extension module, it likely
is a new-style class (i.e. a type).

Most likely, something is wrong with your tp_init slot. You
shouldn't be able to call Context.__init__(): that should
raise a type error, indicating that an object is needed
for the method __init__. That should hold whether Context
is a new-style or an old-style class.

HTH,
Martin
Jan 27 '07 #2
On Jan 27, 6:11 am, Jeff Rush <j...@taupro.comwrote:
While I have a reasonable understanding of the differences in new-style versus
old-style classes, tonight while working a C extension module I realized I
don't know how to indicate which style my C extension module should appear as.

I'm following the Python docs for extended modules, but it doesn't say in
there anyplace I can find which style I'm creating. My clue that something
was wrong is when this:

from cextension import Context

class MyContext(Context):

def __init__(self):
super(Context, self).__init__()

repeatedly and reliably failed with a corrupted C data structure, while this:

class MyContext(Context):

def __init__(self):
Context.__init__()

worked without any problems. As I understand it, the former uses new-style
semantics while the latter uses old-style, and -thats- when I realized I have
no idea which my C extension implemented.

Any enlightenment?
Short answer:

It has nothing to do with old-style classes, and is probably just due
to a mistake in your extension.

Longer answer:

C extention types have never implemented old-style classes. Before
Python 2.2, classes and types were different things. All class
instances were of the same type. However, C extension objects were a
different type, and weren't class instances at all.

Nowadays, new-style classes are types, but the converse isn't
necessary true. In particular, the typical way of defining old
extension types didn't work as a new-style type. So, as part of type-
class unification, they added some new fields (tp_members, tp_new, and
so on) and new idioms to replace some of the old idioms, effectively
creating "new-style types".

Unless you've adopted the new ways, your type isn't going to work as a
class. If you have adopted the new ways, then it's probably just a
mistake in your code.

I suspect you have adopted the new ways, because I'd expect Python to
raise TypeError when trying to subclass a function. So you probably
just have a bug.
Carl Banks

Jan 27 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Pierre Rouleau | last post by:
I have a GUI application written in Python (with WxPython) which uses some low level classes also written in Python. These low level Python classes are given a pointer to some GUI Python object. ...
7
by: Bo Peng | last post by:
Dear Python group: I am planning on an application that involves several complicated C++ classes. Basically, there will be one or two big data objects and some "action" objects that can act on...
1
by: Eric Wilhelm | last post by:
Hi, By new-style classes, I'm referring to the changes which came into 2.2 as a result of PEP 252 and 253. I discovered this problem when trying to use the Perl Inline::Python module with a...
12
by: David MacQuigg | last post by:
I have what looks like a bug trying to generate new style classes with a factory function. class Animal(object): pass class Mammal(Animal): pass def newAnimal(bases=(Animal,), dict={}):...
108
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would...
38
by: looping | last post by:
For Python developers around. >From Python 2.5 doc: The list of base classes in a class definition can now be empty. As an example, this is now legal: class C(): pass nice but why this...
12
by: Ilias Lazaridis | last post by:
Another topic has raised the need of a deeper teach-in. Where can I find _compact_ documentation about * Differece between New Style / Old Style Classes Are there any documents available...
26
by: momobear | last post by:
hi, I am puzzled about how to determine whether an object is initilized in one class, anyone could give me any instructions? here is an example code: class coffee: def boil(self): self.temp =...
11
by: allendowney | last post by:
Hi All, I am working on a revised edition of How To Think Like a Computer Scientist, which is going to be called Think Python. It will be published by Cambridge University Press, but there...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.