Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Computer programs expand so as to fill the core available.


devel / comp.lang.python / Re: Using my routines as functions AND methods

SubjectAuthor
o Re: Using my routines as functions AND methodsGuenther Sohler

1
Re: Using my routines as functions AND methods

<mailman.125.1704369122.2892.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=24785&group=comp.lang.python#24785

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.furie.org.uk!usenet.goja.nl.eu.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: guenther...@gmail.com (Guenther Sohler)
Newsgroups: comp.lang.python
Subject: Re: Using my routines as functions AND methods
Date: Thu, 4 Jan 2024 12:51:48 +0100
Lines: 94
Message-ID: <mailman.125.1704369122.2892.python-list@python.org>
References: <CAASeUHrrsPPe2sqDFjZRLdYTuWoUg1uWNzKRz5QKAhA=T2E0ew@mail.gmail.com>
<CAASeUHqCxg58LrD7sk1c-yMxYEUkApAk2cZfaUYBRTudHMzxuA@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de j3W6qmBt9UFL1ge2vbx5DQCxcDUnCCuaKvt2J+DDtR9g==
Cancel-Lock: sha1:CQFVpXnlS+y+Kf7ZPm7xPjKuDm8= sha256:YCl/y/N7Jr6IM0/MhKXxbTkL+3pue3TjDsVmvdDX5IM=
Return-Path: <guenther.sohler@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=QrnyBebB;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'looks': 0.02; 'this:':
0.03; 'def': 0.04; 'traceback': 0.04; '(most': 0.05; 'last):':
0.05; 'int': 0.09; 'methods,': 0.09; 'typeerror:': 0.09; 'syntax':
0.15; '&lt;': 0.16; '-&gt;': 0.16; '2024': 0.16; 'c++': 0.16;
'classes,': 0.16; 'cpython': 0.16; 'lambda': 0.16; 'reference,':
0.16; 'reuse': 0.16; 'structure.': 0.16; 'tuple': 0.16; 'wrote:':
0.16; 'problem': 0.16; 'python': 0.16; 'probably': 0.17; 'uses':
0.19; 'bug': 0.19; 'to:addr:python-list': 0.20; 'written': 0.22;
'returns': 0.22; 'goal': 0.23; 'idea': 0.24; 'cannot': 0.25;
'function': 0.27; 'approach': 0.31; 'modify': 0.31; '"",': 0.32;
'(this': 0.32; 'subject:Using': 0.32; 'message-
id:@mail.gmail.com': 0.32; 'but': 0.32; 'there': 0.33; 'same':
0.34; 'fill': 0.34; 'header:In-Reply-To:1': 0.34;
'received:google.com': 0.34; 'item': 0.35; 'from:addr:gmail.com':
0.35; 'functions': 0.36; 'using': 0.37; 'class': 0.37; 'file':
0.38; '8bit%:14': 0.38; 'quite': 0.39; 'list': 0.39; 'use': 0.39;
'methods': 0.39; 'wed,': 0.39; 'appears': 0.40; 'want': 0.40;
'try': 0.40; 'method': 0.61; 'skip:o 10': 0.61; 'skip:\xc2 10':
0.62; 'skip:* 20': 0.62; 'skip:o 20': 0.63; 'great': 0.63; '100%':
0.66; 'reach': 0.69; '1);': 0.84; 'attribute': 0.84; 'obj': 0.84;
'one:': 0.84; 'side.': 0.84; 'i++)': 0.91; 'that:': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1704369120; x=1704973920; darn=python.org;
h=to:subject:message-id:date:from:in-reply-to:references:mime-version
:from:to:cc:subject:date:message-id:reply-to;
bh=R6kqfSSFBVdRiLgMFrn8odE/k6yR5EzzDNHQjXdEiXQ=;
b=QrnyBebBJ54WN19Fj2UJT2VK81aI+70jzZS5j7x2R8g8oIsjZvzfKki6YU4k3SCTgo
yHqCuudpNeE4Y9h3eFH20jSDLre5lNKShKcKBrRmfOM6o8mBmG7ameZTqPO/pIJO0K6X
tmu/LweTbiVpzvaCCfeXKCiITVVj3a8J74lx71SkGvdd/aBIeMsXe7iudynvIUZa17ks
7ZCJWXbGwIYLsJ9sGBmv3Dh5BGDPEarHbPrTFnro9rC1xYx4MJ71S128i1vp2FhXa1DI
PgfFD4riCtDGNK+ItdtCBEqdC6ukJ6ziFE4nNmcvDLrkPRz3BuR2rZ9d66nI/KJPEb8Q
SQiA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1704369120; x=1704973920;
h=to:subject:message-id:date:from:in-reply-to:references:mime-version
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
bh=R6kqfSSFBVdRiLgMFrn8odE/k6yR5EzzDNHQjXdEiXQ=;
b=LCVHd0bUjK6ha+zbxbxcyqFWH5hiOKlfrq19eyadO9wdWcR59YLCuEoC9FAZE8L2S0
N6yXDwpsGhWI0nV+r7hbMp26AuI+Fu0TmGiqQZREnxlj2q+QsyN4vvB9h7zBC6DpU4dr
6yL3zezmm7DsRDtn60+5odA3p08ofaEWvlRkBNQno5Vr0glI9wv6XtLCXBCVcEzaFy5I
ykkIOEXCjMWE6l5u+OULzoRCOZlaIMm80Ut43rj2lSCO1NlGa1TacSxee/lc9yBPwhP3
GdwZww1rvK4IZubm1OESxD3M8xgoTGVtVGzaYz49+gH9M0HkB3WmC3Hy6Fudh42hl5cn
zxCQ==
X-Gm-Message-State: AOJu0YxYg0yjuaJEeM8lorhoU4YCYuqPJQoRjZfzgJ/bon4Q3W8SPRzq
ah+VaC0BqCmV3L0l4FsCS7umjvrcnK/ukGFQ/wUUjP/GR0DupA==
X-Google-Smtp-Source: AGHT+IHxqkLOdYL9F9tlSPhSdbzk+Upvfpe8p92hJnUETQmMXJ80NbVCHShS1F0KnDzpdVVOmuIg5atFv2oNtZTkxdQ=
X-Received: by 2002:a17:907:76fb:b0:a28:9d77:46de with SMTP id
kg27-20020a17090776fb00b00a289d7746demr136146ejc.256.1704369120224; Thu, 04
Jan 2024 03:52:00 -0800 (PST)
In-Reply-To: <CAASeUHrrsPPe2sqDFjZRLdYTuWoUg1uWNzKRz5QKAhA=T2E0ew@mail.gmail.com>
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
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: <https://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>
X-Mailman-Original-Message-ID: <CAASeUHqCxg58LrD7sk1c-yMxYEUkApAk2cZfaUYBRTudHMzxuA@mail.gmail.com>
X-Mailman-Original-References: <CAASeUHrrsPPe2sqDFjZRLdYTuWoUg1uWNzKRz5QKAhA=T2E0ew@mail.gmail.com>
 by: Guenther Sohler - Thu, 4 Jan 2024 11:51 UTC

Hi list

The approach with defining the methods from python appears to be a very
good idea and it also works for classes, defined in python side.
However, when I try this one:

def mytrans(self):
print(self)

c=cube()
cls=c.__class__

cls.trans=mytrans

I get this:

Traceback (most recent call last):

File "<string>", line 8, in <module>

TypeError: cannot set 'trans' attribute of immutable type 'PyOpenSCAD'

The Problem is probably that PyOpenSCAD is a class not defined on python
side but on c++ side,
I fear that I need to solution in c++ side. ...

On Wed, Jan 3, 2024 at 11:47 PM Guenther Sohler <guenther.sohler@gmail.com>
wrote:

> Hi,
>
> In my cpython i have written quite some functions to modify "objects".
> and their python syntax is e.g.\
>
> translate(obj, vec). e.g whereas obj is ALWAYS first argument.
>
> on c side this functions looks like:
> PyObject *python_translate(PyObject *self, PyObject *args, PyObject
> *kwargs)
>
> this works great when specifying a list of functions in PyModuleDef
> structure.
>
> However, I also want to use these functions as class methods without
> having to
> write the function , twice. When using the SAME function as a methos, the
> args tuple must insert/contain "self" in the first location, so i have
> written a function to do that:
>
> PyObject *python_oo_args(PyObject *self, PyObject *args) // returns new
> reference,
> {
> int i;
> PyObject *item;
> int n = PyTuple_Size(args);
> PyObject *new_args = PyTuple_New(n + 1);
> PyTuple_SetItem(new_args, 0, self);
>
> for (i = 0; i < PyTuple_Size(args); i++) {
> item = PyTuple_GetItem(args, i);
> PyTuple_SetItem(new_args, i + 1, item);
> }
> return new_args;
> }
>
> To fill in method array, i have created a #define like this:
>
> #define OO_METHOD_ENTRY(name,desc) \
> { #name, (PyCFunction) ( [ ] (PyObject *self, PyObject *args) ->
> PyObject * { \
> PyObject *new_args = python_oo_args(self, args); \
> PyObject *result = python_##name(self, new_args, NULL); \
> return result; } ), METH_VARARGS | METH_KEYWORDS, (desc)},
>
> (this uses a lambda function)
>
> and use this in the array as:
>
> PyMethodDef PyOpenSCADMethods[] = {
> OO_METHOD_ENTRY(translate,"Move Object")
> OO_METHOD_ENTRY(right,"Right Object")
>
> Using this i can reuse all the functions as methods,
> but its not 100% stable/bulletproof and crashes sometimes.
> So there is the bug ?
> Is there a better approach to reach my goal ?
>
>
> thank you
>
>

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor