Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Terry Reedy Newsgroups: comp.lang.python Subject: Re: Writing SOME class methods in C Date: Wed, 18 Nov 2015 04:47:56 -0500 Lines: 42 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de ENntQ3/Rlfzul0y7XrXPXAbA3pK/x1nLEWcehE6WAcCQ== 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; 'python,': 0.02; 'importerror:': 0.05; 'imported': 0.09; 'importerror': 0.09; 'optional': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:Writing': 0.09; 'python': 0.10; 'python.': 0.11; 'jan': 0.11; '(but': 0.15; '.py': 0.16; 'mixture': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'reedy': 0.16; 'statement.': 0.16; 'stdlib.': 0.16; 'subject:class': 0.16; 'too).': 0.16; 'wrote:': 0.16; 'string': 0.17; 'try:': 0.18; 'class,': 0.22; 'pass': 0.22; 'trying': 0.22; 'am,': 0.23; 'defined': 0.23; 'daniel': 0.23; 'import': 0.24; 'examples': 0.24; 'implemented': 0.24; 'written': 0.24; 'header :In-Reply-To:1': 0.24; 'module': 0.25; "i've": 0.25; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'define': 0.27; 'separate': 0.27; 'least': 0.27; 'this.': 0.28; 'objects': 0.29; "i'm": 0.30; 'classes': 0.30; 'putting': 0.30; "can't": 0.32; 'implement': 0.32; 'run': 0.33; 'class': 0.33; 'consist': 0.33; 'except': 0.34; 'done': 0.35; 'replace': 0.35; 'but': 0.36; 'there': 0.36; 'modules': 0.36; 'received:71': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'whatever': 0.39; 'to:addr:python.org': 0.40; 'hello,': 0.40; 'some': 0.40; 'entire': 0.61; 'believe': 0.66; 'present.': 0.72; 'optional:': 0.84; 'received:fios.verizon.net': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: pool-71-185-227-36.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:98954 On 11/18/2015 2:50 AM, Daniel Haude wrote: > Hello, > > I'm trying to implement some (but not all) methods of a Python class in C. > What I've found on the Net is: > - how to implement entire modules in C so that I can import that module and > use the C functions (successfully done it, too). > - how to implement entire classes in C > > But I can't find any examples of modules which consist of a mixture of C and > Python, There at least to be such in the stdlib. The .py module defined *everything* and then ended with try: from _module import * except ImportError: pass to replace whatever top-level objects were also written in C. The try-except part is optional but let the module run when _module was not present. I believe the string module was once like this. > nor modules that define classes of which some members are > implemented in C, others in Python. I would try putting the C part in separate base or mix-in class, imported before the class statement. To make the C part optional: class mixin: ... try: from _module import mixin except ImportError pass class myclass(mixin): ... -- Terry Jan Reedy