Python C-API: how to define nested classes?

2013-05-16 Thread Serge WEINSTOCK
Hi,

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?

Serge WEINSTOCK


___
This e-mail may contain confidential and/or privileged information. If you are 
not the intended recipient (or have received this e-mail in error) please 
notify the sender immediately and delete this e-mail. Any unauthorised copying, 
disclosure or distribution of the material in this e-mail is prohibited.

Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for additional 
disclosures.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python C-API: how to define nested classes?

2013-05-16 Thread 88888 Dihedral
Serge WEINSTOCK於 2013年5月16日星期四UTC+8下午4時55分07秒寫道:
 Hi,
 
  
 
 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?
 
  
 
 Serge WEINSTOCK
 
  
 
 
 
 
 ___
 
 This e-mail may contain confidential and/or privileged information. If you 
 are not the intended recipient (or have received this e-mail in error) please 
 notify the sender immediately and delete this e-mail. Any unauthorised 
 copying, disclosure or distribution of the material in this e-mail is 
 prohibited.
 
 
 Please refer to http://www.bnpparibas.co.uk/en/email-disclaimer/ for 
 additional disclosures.

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 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python C-API: how to define nested classes?

2013-05-16 Thread Stefan Behnel
Serge WEINSTOCK, 16.05.2013 10:55:
 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?

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.

Stefan


-- 
http://mail.python.org/mailman/listinfo/python-list