Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

Many changes of mind and mood; do not hesitate too long.


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

SubjectAuthor
o Re: Difference method vs attribut = functionPeter J. Holzer

1
Subject: Re: Difference method vs attribut = function
From: Peter J. Holzer
Newsgroups: comp.lang.python
Date: Sat, 29 Jun 2024 19:53 UTC
References: 1 2
Attachments: signature.asc (application/pgp-signature)
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: hjp-python@hjp.at (Peter J. Holzer)
Newsgroups: comp.lang.python
Subject: Re: Difference method vs attribut = function
Date: Sat, 29 Jun 2024 21:53:05 +0200
Lines: 79
Message-ID: <mailman.180.1719690794.2909.python-list@python.org>
References: <20240628180854.7ee713db744e9672ad668f36@fam-goebel.de>
<20240629195305.kxzys5ht7hottfup@hjp.at>
Mime-Version: 1.0
Content-Type: multipart/signed; micalg=pgp-sha512;
protocol="application/pgp-signature"; boundary="d3b2qja7gqav7z6p"
X-Trace: news.uni-berlin.de 5FQ9eP4huGpP78wFfHqHYgyQEXapYTLQxCl76ssRM5zQ==
Cancel-Lock: sha1:ZzYlUfl9QpFanitbOIWwnmXnlHg= sha256:eAOJUmDSeQPnYFKXikhkAb6YjiIQl7ZoWHzW6bDA6I8=
Return-Path: <hjp-python@hjp.at>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.003
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'def': 0.04; '(e.g.': 0.05;
'content-type:multipart/signed': 0.05; 'maintainers': 0.07;
'content-type:application/pgp-signature': 0.09; 'filename:fname
piece:asc': 0.09; 'filename:fname piece:signature': 0.09;
'filename:fname:signature.asc': 0.09; 'instances': 0.09;
'methods,': 0.09; 'question:': 0.09; 'import': 0.15; '"creative':
0.16; '+0200,': 0.16; '__/': 0.16; 'attributes,': 0.16;
'challenge!"': 0.16; 'from:addr:hjp-python': 0.16;
'from:addr:hjp.at': 0.16; 'from:name:peter j. holzer': 0.16;
'hjp@hjp.at': 0.16; 'holzer': 0.16; 'reality.': 0.16; 'stross,':
0.16; 'subject: = ': 0.16; 'url-ip:212.17.106/24': 0.16; 'url-
ip:212.17/16': 0.16; 'url:hjp': 0.16; '|_|_)': 0.16; 'wrote:':
0.16; 'to:addr:python-list': 0.20; 'skip:_ 10': 0.22; 'code':
0.23; 'object': 0.26; 'function': 0.27; 'sense': 0.28; "doesn't":
0.32; 'context': 0.32; 'course.': 0.32; 'do.': 0.32; 'python-
list': 0.32; 'but': 0.32; 'there': 0.33; 'hold': 0.33; 'header:In-
Reply-To:1': 0.34; 'functions': 0.36; 'main': 0.37; 'class': 0.37;
'way': 0.38; 'could': 0.38; 'use': 0.39; 'both': 0.40; 'want':
0.40; 'should': 0.40; 'method': 0.61; 'here.': 0.61;
'received:212': 0.62; 'here': 0.62; 'skip:m 20': 0.63; 'pass':
0.64; 'well': 0.65; 'skip:t 20': 0.66; 'received:userid': 0.66;
'shows': 0.67; 'types': 0.67; 'per': 0.68; 'subject:method': 0.69;
'url-ip:212/8': 0.69; 'future': 0.72; 'yourself': 0.75;
'database': 0.80; 'attribute': 0.84; 'capture': 0.84; 'closure':
0.84; 'method,': 0.84; 'received:at': 0.84
Mail-Followup-To: python-list@python.org
Content-Disposition: inline
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: <20240629195305.kxzys5ht7hottfup@hjp.at>
X-Mailman-Original-References: <20240628180854.7ee713db744e9672ad668f36@fam-goebel.de>
View all headers

On 2024-06-28 18:08:54 +0200, Ulrich Goebel via Python-list wrote:
> 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,

The other way around: It includes only the attributes, not the methods.

> 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")

Here is the other main difference: The object is not passed implicitely
to the function. You have no way to access mc here.

You can create a method on the fly with types.MethodType:

import types
mc.functionAttribute = types.MethodType(function, mc)

> 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.

Or in this case, since each function is specific to one instance, you
could just use a closure to capture the object. But that might be
confusing to any future maintainers (e.g. yourself in 6 months), if the
method doesn't actually behave like a method.

hp

--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | hjp@hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"

Attachments: signature.asc (application/pgp-signature)
1

rocksolid light 0.9.8
clearnet tor