top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Python C-API: how to define nested classes?

0 votes
331 views

I'm currently writing a C extension module for python using the "raw" C-API. I would like to be able to define "nested classes" like in the following python code

class A: 
   class B: 
     def __init__(self): 
     self.i = 2 
     def __init__(self): 

     self.b = A.B() 
     a = A() 
     b = A.B() 
     print(a.b.i) 
print(b.i) 

How can I create such nested class with the C-API?

posted May 16, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes

The nested class is just defining the scope of the nested class object that can be used solely in all outer proper classes.

It is not very useful in a language which supports dynamical run-time attribute binding in the instance level in the outer

answer May 16, 2013 by anonymous
0 votes

Assuming you really mean Python classes and not extension types, you might get away with defining them separately and then assigning

A.B = B

Recent Python versions added a __qualname__ attribute for classes that you might want to fix up in this case.

If you need better compatibility, consider writing your code in Cython.

answer May 17, 2013 by anonymous
Similar Questions
+1 vote

I have C-Python API like below. It works fine, but the problem is while invoking this method from python script say

#cat script.py

offset=0
size=4
write_object(offset,size)

This calls write_this_c() C api and returns quickly to next printf statement. But the return call (Py_RETURN_NONE) takes something like 4-6 seconds.

static
PyMethodDef xyz_methods[] = {
{"write_object", write_object, METH_VARARGS,"write some stuff "},
{NULL, NULL, 0, NULL}
};
....

static PyObject *
write_object(PyObject *self, PyObject *args)
{
 int offset, size;
 if (!PyArg_ParseTuple(args,"ii", &offset, 

 printf("before call");
 write_this_c(offset, size);
 printf("after call");
 Py_RETURN_NONE; ##delay happens here
}

How to avoid this delay time?

0 votes

I use: Python 2.6 and sqlalchemy 0.6.1

This is what I am trying to do:

 from sqlalchemy.types import (
 Integer,
 String,
 Boolean
 )
 from sqlalchemy.ext.declarative import declarative_base

 Base = declarative_base()

 class SampleMeta(type):
 def __new__(cls, name, bases, attrs):
 attrs.update({ 'id': Column('Id', Integer, primary_key=True),
 'name': Column('Name', String),
 'description': Column('Description', String),
 'is_active': Column('IsActive', Boolean)
 })
 return super(SampleMeta, cls).__new__(cls, name, bases, attrs)

 class Sample(Base):
 __tablename__ = 'Sample'
 __table_args__ = {'useexisting': True}
 __metaclass__ = SampleMeta

 def __init__(self, id, name, description, is_active):
 self.id = id
 self.name = name
 self.description = description
 self.is_active = is_active

 def __repr__(self):
 return "" % (self.id, self.name, self.description, self.isactive)

And the error I am getting is this:

 TypeError: Error when calling the metaclass bases
 metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

Now, if I do the same thing above by using

 class Sample(object)

instead of

 class Sample(Base)

it works absolutely fine.

I need to update the attributes of the class dynamically. So, I will be using dynamic attribute and column names. And I need the above piece code to work in order to be able to get there.

0 votes

This is my dilemma, Im trying to get the generated JSON file using the bing api search.

This is the code that Im executing from inside the shell:
http://bin.cakephp.org/view/460660617 [1]

The port doesnt matter to me. Thoughts?

+1 vote

If we write Class in windows, can we use them without any modifications in Linux, if so then what about GUI programming and Libraries.

0 votes

We have few rules (changeable) are stored in a database, and I need to display them for the end user in the form of flow-chart.

Is there such thing for Python?

...