Deutsch   English   Français   Italiano  
<attributes-20240629214445@ram.dialup.fu-berlin.de>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: Difference method vs attribut = function
Date: 29 Jun 2024 20:48:58 GMT
Organization: Stefan Ram
Lines: 45
Expires: 1 Feb 2025 11:59:58 GMT
Message-ID: <attributes-20240629214445@ram.dialup.fu-berlin.de>
References: <mailman.179.1719687158.2909.python-list@python.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de Mjt/HKY+mash8VhbQdpM1wHxpV+waU+Y88HkCmKbi7ZA9E
Cancel-Lock: sha1:taowRxxOvJyfFFjBsI24mL8RyJo= sha256:2G7iaydvrn1ffI+myiFekVzh0le3u+ZD9kEgw2y1olw=
X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved.
	Distribution through any means other than regular usenet
	channels is forbidden. It is forbidden to publish this
	article in the Web, to change URIs of this article into links,
        and to transfer the body without this notice, but quotations
        of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
	services to mirror the article in the web. But the article may
	be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Bytes: 2317

Ulrich Goebel <ml@fam-goebel.de> wrote or quoted:
>a class can have methods, and it can have attributes, which
>can hold a function. Both is well known, of course.

  Methods are attributes too:

  Main.py

class c:
    def m():
        pass

print( *( name for name in dir( c )if not name.startswith( '__' )))

  sys.stdout

m

  .

>class MyClass:
>    def __init__(self):
>        functionAttribute = None

  Your local name "functionAttribute" is not an attribute of "MyClass".
 
>mc = MyClass()
>mc.functionAttribute = function

  Above, you only add an attribute to "mc", not to "MyClass".

>By the way: in my usecase I want to pass different functions
>to different instances of MyClass.

class MyClass:
    def __init__( self ):
        self.function = None
    def accept( self, function ):
        self.function = function

mc = MyClass()
mc.accept( print )

mc_1 = MyClass()
mc_1.accept( dir )