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


Groups > comp.lang.python > #98954

Re: Writing SOME class methods in C

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From Terry Reedy <tjreedy@udel.edu>
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 <mailman.407.1447840093.16136.python-list@python.org> (permalink)
References <slrnn4obfa.db7.dh@dotcom.mfs32>
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 <python-python-list@m.gmane.org>
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 <slrnn4obfa.db7.dh@dotcom.mfs32>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:98954

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Writing SOME class methods in C Daniel Haude <dh@dotcom.mfs32> - 2015-11-18 07:50 +0000
  Re: Writing SOME class methods in C Terry Reedy <tjreedy@udel.edu> - 2015-11-18 04:47 -0500
  Re: Writing SOME class methods in C Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2015-11-18 12:52 +0000
  Re: Writing SOME class methods in C Stefan Behnel <stefan_ml@behnel.de> - 2015-11-29 14:26 +0100

csiph-web