Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #351: PEBKAC (Problem Exists Between Keyboard And Chair)


comp / comp.lang.python / Re: Difference method vs attribut = function

SubjectAuthor
o Re: Difference method vs attribut = functionThomas Passin

1
Subject: Re: Difference method vs attribut = function
From: Thomas Passin
Newsgroups: comp.lang.python
Date: Sat, 29 Jun 2024 20:01 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: list1@tompassin.net (Thomas Passin)
Newsgroups: comp.lang.python
Subject: Re: Difference method vs attribut = function
Date: Sat, 29 Jun 2024 16:01:49 -0400
Lines: 58
Message-ID: <mailman.182.1719699957.2909.python-list@python.org>
References: <20240628180854.7ee713db744e9672ad668f36@fam-goebel.de>
<02720cd8-690a-41aa-afaa-548a7430c791@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 WEKH4phCYheQHsnSNeArIgploQQLa2xkGzyia6Llq5dw==
Cancel-Lock: sha1:GvuO9Kh0KUqhLNtMGiat02Vz61w= sha256:k7OQXhk7S5H/eyCrgs7an+F/CdsNAKLqNKG+lhsnbK4=
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=gmczKiZk;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.036
X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; 'def': 0.04; 'instances':
0.09; 'methods,': 0.09; 'question:': 0.09; 'attributes': 0.16;
'attributes,': 0.16; 'hints': 0.16; 'instance': 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; 'subject: = ': 0.16; 'wrote:':
0.16; 'pm,': 0.19; 'to:addr:python-list': 0.20; 'skip:_ 10': 0.22;
'code': 0.23; 'object': 0.26; 'function': 0.27; 'header:User-
Agent:1': 0.30; 'attempt': 0.31; 'context': 0.32; 'course.': 0.32;
'do.': 0.32; 'python-list': 0.32; 'received:10.0': 0.32;
'received:mailchannels.net': 0.32;
'received:relay.mailchannels.net': 0.32; 'but': 0.32; 'there':
0.33; 'hold': 0.33; 'header:In-Reply-To:1': 0.34; 'functions':
0.36; 'class': 0.37; 'thanks': 0.38; 'valid': 0.39; 'list': 0.39;
'both': 0.40; 'want': 0.40; 'should': 0.40; 'method': 0.61;
'skip:o 10': 0.61; 'skip:m 20': 0.63; 'pass': 0.64; 'well': 0.65;
'shows': 0.67; 'header:Received:6': 0.67; 'received:64': 0.67;
'per': 0.68; 'subject:method': 0.69; '....': 0.76; 'database':
0.80; 'attribute': 0.84; 'method,': 0.84
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1719691310; a=rsa-sha256;
cv=none;
b=wgeX4vgt+L6n0FB0qwomTU13mqPsCLNRGPStkaZiOLR1tnyu/F2rvS1bHt5ez5AvI1xOee
TD96fDki/NclE/g+j6lf0a9DO6aInowaBN9AqJB01xTOBUisKuGjAhwSbv6vubrlaymF4o
mptAQlQYg1nbhE8guj2rNoLvgYjESVwHaojl+QiFmBfmBk9GkDX58qxJs/qPQvQgoLbAMf
lcDC8B7+8/OczzTGN6q3VFXmpmkDH086bTMhgVzbDaDaQXEiJxfLw9ZLhX9oR+19SREGNc
eLjAH74Owcv1Woi3TGSC1bAcGDWQPwplY60D1IAgi5J+moBqEjauAbbkYMVD9g==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed;
d=mailchannels.net; s=arc-2022; t=1719691310;
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=yPa7lEnJx4vDftYFqpkdadgEvi88mrZCQH5cFWeb+mU=;
b=kWZNSSAnkaPIZdxu4BTIFJo9EWYUohG8904rbMwxszNC8Zg18CG+l6XsCuBQf6Zqhbz8Cd
2kMvzJX2kODA41SVtOsNNpn7AAVIWkx+OMWZg2SfEJaL4HkTGeIr+0nKq0cr5MPNZvsn5N
34I7sCQtHbHaJxNwJSfPJuCtFB6xJM416tMWj5HQY5EC9LNGonaReHnTfzy/n5u5Ol0csZ
+OTwKVwK4APxbTgXKn/slIRLUHi1o+Lq1lsg+6+/csy5xUwiMZtHafLcWeH3xa1Okdk+d5
Calfb2AJEgNNicb50fMcvQPubTbx/zDWNEXgjyWb3S7oQy9Udt4USpv0I2lAYw==
ARC-Authentication-Results: i=1; rspamd-7f76976655-l9qrj;
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-Ski-Keen: 121be2640bdcd626_1719691310601_3004502061
X-MC-Loop-Signature: 1719691310601:4202130037
X-MC-Ingress-Time: 1719691310600
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tompassin.net;
s=dreamhost; t=1719691310;
bh=K1aIJmmJNxdcmQ7dTc0fSYNTOJwvHYq3gZ1jyjlf/EA=;
h=Date:Subject:To:From:Content-Type:Content-Transfer-Encoding;
b=gmczKiZkc2BbkaV5AmT+nATJaNFTLfGvBNKdAKEORqAXEo5evqYfM93+PBlKdz7sE
1VgM0vbNjjy19sjzUHU7JP4h0MUpk8Yyh6BTqvc8ZPcxu+JGgQnw4XqnHuPHZEy9qY
eNleZlLvvwWHpHxVNbFhoD6SDXlGMSlkmelEw3TdUepOxu82n5z87tQb8yIuYSzaU3
pNBRqHcgnflkRcEO/7gFEQFLd93kIRLMekAi4rFU3cnwVNjNGGwvTnOlbfCfSRSwnC
KuEE/jfYtMRCnOyRDetiIs/P2FkeBLFDgUTrcP9HLpljBMxeAryaoRiclTcTT/fN8t
r2AJzJ9rgcXLQ==
User-Agent: Mozilla Thunderbird
Content-Language: en-US
In-Reply-To: <20240628180854.7ee713db744e9672ad668f36@fam-goebel.de>
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: <02720cd8-690a-41aa-afaa-548a7430c791@tompassin.net>
X-Mailman-Original-References: <20240628180854.7ee713db744e9672ad668f36@fam-goebel.de>
View all headers

On 6/28/2024 12:08 PM, Ulrich Goebel via Python-list wrote:
> Hi,
>
> a class can have methods, and it can have attributes, which can hold a function. Both is well known, of course.
>
> My question: Is there any difference?
>
> The code snipped shows that both do what they should do. But __dict__ includes just the method, while dir detects the method and the attribute holding a function. My be that is the only difference?
>
>
> class MyClass:
> def __init__(self):
> functionAttribute = None
>
> def method(self):
> print("I'm a method")
>
> def function():
> print("I'm a function passed to an attribute")
>
> mc = MyClass()
> mc.functionAttribute = function
>
> mc.method()
> mc.functionAttribute()
>
> print('Dict: ', mc.__dict__) # shows functionAttribute but not method
> print('Dir: ', dir(mc)) # shows both functionAttribute and method
>
>
> By the way: in my usecase I want to pass different functions to different instances of MyClass. It is in the context of a database app where I build Getters for database data and pass one Getter per instance.
>
> Thanks for hints
> Ulrich

https://docs.python.org/3/library/functions.html#dir -

object.__dict__¶
A dictionary or other mapping object used to store an object’s
(writable) attributes.

dir(object)
....
With an argument, attempt to return a list of valid attributes for that
object.

"functionAttribute" is a class method, not an instance method. If you
want an instance method:

class MyClass:
def __init__(self):
functionAttribute = None
self.instance_functionAttribute = None

def method(self):
print("I'm a method")

1

rocksolid light 0.9.8
clearnet tor