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


Groups > comp.lang.python > #196346

Difference method vs attribut = function

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ulrich Goebel <ml@fam-goebel.de>
Newsgroups comp.lang.python
Subject Difference method vs attribut = function
Date Fri, 28 Jun 2024 18:08:54 +0200
Lines 37
Message-ID <mailman.179.1719687158.2909.python-list@python.org> (permalink)
References <20240628180854.7ee713db744e9672ad668f36@fam-goebel.de>
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de KfBiub0UxuE3mmIN1g69DwbEZ6CbxE28NBrdXUXBNKog==
Cancel-Lock sha1:TXQG0U6W15PS/az1nUTtDrSb9ro= sha256:xjonGW1dn53oGewfQ2Hu5TCjHoTRTvOBk79xkzNjcyQ=
Return-Path <ml@fam-goebel.de>
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.036
X-Spam-Evidence '*H*': 0.93; '*S*': 0.00; 'def': 0.04; 'instances': 0.09; 'methods,': 0.09; 'question:': 0.09; 'received:78': 0.09; 'attributes,': 0.16; 'hints': 0.16; 'received:136.243': 0.16; 'received:78.46': 0.16; 'received:your-server.de': 0.16; 'subject: = ': 0.16; 'to:addr:python-list': 0.20; 'skip:_ 10': 0.22; 'code': 0.23; 'received:de': 0.23; 'function': 0.27; 'context': 0.32; 'course.': 0.32; 'do.': 0.32; 'received:136': 0.32; 'but': 0.32; 'there': 0.33; 'hold': 0.33; 'functions': 0.36; 'class': 0.37; 'thanks': 0.38; 'both': 0.40; 'want': 0.40; 'should': 0.40; 'method': 0.61; 'skip:m 20': 0.63; 'pass': 0.64; 'well': 0.65; 'shows': 0.67; 'per': 0.68; 'subject:method': 0.69; 'database': 0.80; 'attribute': 0.84; 'method,': 0.84
X-Mailer Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-pc-linux-gnu)
X-Authenticated-Sender ml@fam-goebel.de
X-Virus-Scanned Clear (ClamAV 0.103.10/27320/Fri Jun 28 10:37:18 2024)
X-Mailman-Approved-At Sat, 29 Jun 2024 14:52:38 -0400
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 <20240628180854.7ee713db744e9672ad668f36@fam-goebel.de>
Xref csiph.com comp.lang.python:196346

Show key headers only | View raw


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


-- 
Ulrich Goebel <ml@fam-goebel.de>

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


Thread

Difference method vs attribut = function Ulrich Goebel <ml@fam-goebel.de> - 2024-06-28 18:08 +0200
  Re: Difference method vs attribute = function (Posting On Python-List Prohibited) Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-06-29 22:55 +0000

csiph-web