Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

<lilo> Fairlight: udp is the light margarine of tcp/ip transport protocols :) -- Seen on #Linux


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

SubjectAuthor
o Re: Using my routines as functions AND methodsThomas Passin

1
Re: Using my routines as functions AND methods

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!paganini.bofh.team!2.eu.feeder.erje.net!feeder.erje.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: lis...@tompassin.net (Thomas Passin)
Newsgroups: comp.lang.python
Subject: Re: Using my routines as functions AND methods
Date: Thu, 4 Jan 2024 00:11:16 -0500
Lines: 85
Message-ID: <mailman.121.1704346125.2892.python-list@python.org>
References: <CAASeUHrrsPPe2sqDFjZRLdYTuWoUg1uWNzKRz5QKAhA=T2E0ew@mail.gmail.com>
<62297ce5-c36b-afac-c2f8-6c4f0b412875@yahoo.co.uk>
<0f0b9909-8d94-44ca-bd3f-42cda17dd428@tompassin.net>
<a9aa7ec0-be49-4081-80a8-63ef9dee0326@tompassin.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de rlv0HVWz1O7y7Eo1ZMvIcwVUh7mUB/RKiexWtD4/jUBA==
Cancel-Lock: sha1:OeudhhhB9949BBSaSWaJP6DqxjQ= sha256:1U+q9Sy+p52yH87dSfdMDRlzeKk/5aChfn2or3x8X5U=
Return-Path: <list1@tompassin.net>
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=tompassin.net header.i=@tompassin.net header.b=FlTFnC9t;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'argument': 0.04; 'def':
0.04; 'class.': 0.07; '"""': 0.09; 'meant': 0.09; 'prints': 0.09;
'received:23.83.212': 0.09; 'received:elm.relay.mailchannels.net':
0.09; 'syntax': 0.15; 'that.': 0.15; '8:00': 0.16; 'cpython':
0.16; 'gauld': 0.16; 'member?': 0.16; 'naive': 0.16; 'print(x)':
0.16; 'received:10.0.0': 0.16; 'received:64.90': 0.16;
'received:64.90.62': 0.16; 'received:64.90.62.162': 0.16;
'received:dreamhost.com': 0.16; 'tuple': 0.16; 'wrote:': 0.16;
'python': 0.16; 'probably': 0.17; "can't": 0.17; 'pm,': 0.19;
'to:addr:python-list': 0.20; 'written': 0.22; 'anything': 0.25;
'seems': 0.26; 'tried': 0.26; 'function': 0.27; '>>>': 0.28;
'asked': 0.29; 'header:User-Agent:1': 0.30; 'modify': 0.31;
'think': 0.32; 'else,': 0.32; 'python-list': 0.32;
'received:10.0': 0.32; 'received:mailchannels.net': 0.32;
'received:relay.mailchannels.net': 0.32; 'subject:Using': 0.32;
'but': 0.32; "i'm": 0.33; 'there': 0.33; 'same': 0.34; 'header:In-
Reply-To:1': 0.34; 'complex': 0.35; 'functions': 0.36; 'missing':
0.37; 'using': 0.37; "it's": 0.37; 'class': 0.37; 'quite': 0.39;
'use': 0.39; 'methods': 0.39; 'alan': 0.40; 'explain': 0.40;
'something': 0.40; 'want': 0.40; 'method': 0.61; 'here': 0.62;
'pass': 0.64; 'your': 0.64; 'header:Received:6': 0.67;
'received:64': 0.67; 'obvious': 0.69; 'extra': 0.84; 'obj': 0.84;
'\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0': 0.84; 'that:': 0.91
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1704345079; a=rsa-sha256;
cv=none;
b=4r+NBQgf6oeVfA7vPMaN570R7LYMlze7nBhgcrpfqpADJdkZu7R6ZULujQiV8YInEJ0SMj
AjKxyO1VV0SVlDK8VuXc1fH7lxDXHz7h90S+s75iUlJfabnB3RaWeOJr/WyD9jBx/fh4I3
6mlkpwpjotrJSShtnvyXr92aqLderU40QZfJ6zwl3jxvUXiyZR3zqfyZg17l27i2M5LHDn
QMBbpYaC28bklMfVQXyPzROiG1IVh/Y4hMO/eQzf2l5Rfx7ISa7jECxkuOGnFwcAD/LjXT
r+J60ivDnEFUJ2cNvr9c4NN5PQP267izzTdgaeocQTXQQY5asuhMa7yixonWxA==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed;
d=mailchannels.net; s=arc-2022; t=1704345079;
h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
to:to:cc:mime-version:mime-version:content-type:content-type:
content-transfer-encoding:content-transfer-encoding:
in-reply-to:in-reply-to:references:references:dkim-signature;
bh=zk1apX6SNhO9mr2wzaUOCaIU1rIFN+GBYSTMwK4d4ds=;
b=jKkNRm3bdhyZvmN7PbVMucDRHqkrRgwsHtz/ynnfmQCxpxE50HkqDI0WDVigpe6uUU/R1B
XkpY7FqNomW6rkoKxm8rvySoGHv1dF8Wh8dAUYBnvn7SPrLwJQBbzk/P3w/FN/3pu/PD4P
G6uncK0uNEj5AdcX2dIrWkNz6Tx9w4+Py62QlStV40X/WCapYhJoPRm/8V+h0vWF3BtK3e
a3AvgJSIVvD/y5CPCVtGsK93y59Ddqf+eZE27CNjlXgdTEP//dHwJMvkXz7jSE71eHGkar
DFG+5a0u2KIn8ygq0hRG6DZ7pn7HfRxW3yWlUiyt0JMvHo4oUY86U3LOlAcsgQ==
ARC-Authentication-Results: i=1; rspamd-b89458897-qp4r5;
auth=pass smtp.auth=dreamhost smtp.mailfrom=list1@tompassin.net
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
X-MC-Relay: Neutral
X-MailChannels-SenderId: dreamhost|x-authsender|tpassin@tompassin.net
X-MailChannels-Auth-Id: dreamhost
X-Scare-Turn: 66b3226724272005_1704345080094_2241877026
X-MC-Loop-Signature: 1704345080094:1125863050
X-MC-Ingress-Time: 1704345080093
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tompassin.net;
s=dreamhost; t=1704345079;
bh=zk1apX6SNhO9mr2wzaUOCaIU1rIFN+GBYSTMwK4d4ds=;
h=Date:Subject:From:To:Content-Type:Content-Transfer-Encoding;
b=FlTFnC9tUTpL7TkqW+JOyLi3XqIu2ru/tRRBjDezV7olKirmoJ9LAKjI47GXaPJzh
vQ/nIzTYSaquHssv3yNcKeAdYk0locSBg+EZ/Kigu7WZmnDVhb62IFkGiWd7Emz/Gi
m7PnBshTjQSKnJrNFzxo4UYkh44aazd9CSXwG2WNhEd2kyyYaPj52fZ+SzG8sda5IV
bZ3nNEcAwJOvoJ+gI557HRupKishXxNYzlF5HuLUtSm4Di9zr+R3Ss7xTF6FDLn/lZ
ySs0j5ylmk+8PxZMdN/oee41HzhCyDNZsanIWhPOKl2O7yLNi9NtcwsLQl+DBoGPCA
Itd2XTguXORaA==
User-Agent: Mozilla Thunderbird
Content-Language: en-US
In-Reply-To: <0f0b9909-8d94-44ca-bd3f-42cda17dd428@tompassin.net>
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: <a9aa7ec0-be49-4081-80a8-63ef9dee0326@tompassin.net>
X-Mailman-Original-References: <CAASeUHrrsPPe2sqDFjZRLdYTuWoUg1uWNzKRz5QKAhA=T2E0ew@mail.gmail.com>
<62297ce5-c36b-afac-c2f8-6c4f0b412875@yahoo.co.uk>
<0f0b9909-8d94-44ca-bd3f-42cda17dd428@tompassin.net>
 by: Thomas Passin - Thu, 4 Jan 2024 05:11 UTC

On 1/3/2024 11:17 PM, Thomas Passin wrote:
> On 1/3/2024 8:00 PM, Alan Gauld via Python-list wrote:
>> On 03/01/2024 22:47, Guenther Sohler via Python-list 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.
>>
>>> 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:
>>
>> I'm probably missing something obvious here but can't you
>> just assign your function to a class member?
>>
>> def myFunction(obj, ...): ...
>>
>> class MyClass:
>>      myMethod = myFunction
>>
>>
>> Then you can call it as
>>
>> myObject = MyClass()
>> myObject.myMethod()
>>
>> A naive example seems to work but I haven't tried anything
>> complex so there is probably a catch. But sometimes the simple
>> things just work?
>
> That works if you assign the function to a class instance, but not if
> you assign it to a class.
>
> def f1(x):
>     print(x)
> f1('The plain function')
>
> class Class1:
>     pass
>
> class Class2:
>     pass
>
> c1 = Class1()
> c1.newfunc = f1
> c1.newfunc('f1 assigned to instance') # Works as intended
>
> Class2.newfunc = f1
> c2 = Class2()
> c2.newfunc('f1 assigned to class')  # Complains about extra argument

If your requirements are not very tricky, you can write a
convert-to-method function yourself:

def f1(x):
print(x)
f1('The plain function')

class Class2:
pass

def convert_method(f):
"""Assign existing method without a "self" arg
as a class's method.
"""
def fnew(instance, *args):
f(*args)
return fnew

Class2.newfunc = convert_method(f1)
c2 = Class2()
c2.newfunc('f1 assigned as method of Class2') # Prints the arg

This example does not make f1 aware of the self argument, but you asked
to convert an existing function, and that function would not be aware of
the self parameter. It's much like a decorator function, but is not here
being used as a decorator. If you meant something else, please think out
what you want and explain that.

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor