Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.06; 'attribute': 0.07; 'nested': 0.07; 'assigning': 0.09; 'assuming': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'separately': 0.09; 'python': 0.11; 'def': 0.12; 'a()': 0.16; 'classes"': 0.16; 'from:addr:behnel.de': 0.16; 'from:addr:stefan_ml': 0.16; 'from:name:stefan behnel': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:API': 0.16; 'types,': 0.16; 'fix': 0.17; 'module': 0.19; 'stefan': 0.19; 'header:User- Agent:1': 0.23; 'case.': 0.24; 'versions': 0.24; 'define': 0.26; 'extension': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In- Reply-To:1': 0.27; "i'm": 0.30; 'code': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'classes': 0.35; 'received:84': 0.35; 'really': 0.36; 'subject:?': 0.36; 'to:addr:python-list': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'such': 0.63; 'received:arcor-ip.net': 0.84; 'received:pools .arcor-ip.net': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Stefan Behnel Subject: Re: Python C-API: how to define nested classes? Date: Fri, 17 May 2013 07:16:25 +0200 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: dslb-084-056-018-252.pools.arcor-ip.net User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130404 Thunderbird/17.0.5 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 31 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1368767797 news.xs4all.nl 15893 [2001:888:2000:d::a6]:38564 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:45468 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