Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #45468 > unrolled thread

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

Started byStefan Behnel <stefan_ml@behnel.de>
First post2013-05-17 07:16 +0200
Last post2013-05-17 07:16 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Python C-API: how to define nested classes? Stefan Behnel <stefan_ml@behnel.de> - 2013-05-17 07:16 +0200

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

FromStefan Behnel <stefan_ml@behnel.de>
Date2013-05-17 07:16 +0200
SubjectRe: Python C-API: how to define nested classes?
Message-ID<mailman.1780.1368767797.3114.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web