Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

Q: What do you call a boomerang that doesn't come back? A: A stick.


comp / comp.lang.python / Re: Difference method vs attribute = function (Posting On Python-List Prohibited)

SubjectAuthor
* Difference method vs attribut = functionUlrich Goebel
`* Re: Difference method vs attribut = functionStefan Ram
 `- Re: Difference method vs attribute = function (Posting On Python-List ProhibitedLawrence D'Oliveiro

1
Subject: Difference method vs attribut = function
From: Ulrich Goebel
Newsgroups: comp.lang.python
Date: Fri, 28 Jun 2024 16:08 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: ml@fam-goebel.de (Ulrich Goebel)
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>
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>
View all headers

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>

Subject: Re: Difference method vs attribut = function
From: Stefan Ram
Newsgroups: comp.lang.python
Organization: Stefan Ram
Date: Sat, 29 Jun 2024 20:48 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!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
View all headers

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 )

Subject: Re: Difference method vs attribute = function (Posting On Python-List Prohibited)
From: Lawrence D'Oliv
Newsgroups: comp.lang.python
Organization: A noiseless patient Spider
Date: Sat, 29 Jun 2024 22:55 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.lang.python
Subject: Re: Difference method vs attribute = function (Posting On Python-List
Prohibited)
Date: Sat, 29 Jun 2024 22:55:54 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <v5q3dq$4pv6$2@dont-email.me>
References: <mailman.179.1719687158.2909.python-list@python.org>
<attributes-20240629214445@ram.dialup.fu-berlin.de>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 30 Jun 2024 00:55:55 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="4341965ad4e2fb75a1b93d496a5627b7";
logging-data="157670"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18l2T2a1rlQO7X/tSzIzxuv"
User-Agent: Pan/0.158 (Avdiivka; )
Cancel-Lock: sha1:t5eLVJyS0DG9F6rg9yc66c59OLg=
View all headers

On 29 Jun 2024 20:48:58 GMT, Stefan Ram wrote:

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

They are indeed:

class A:
def set_x(self, x) :
self.x = x
#end set_x
#end A

class B:
pass
#end class B

def set_x(self, x) :
self.x = x
#end set_x

B.set_x = set_x

AInst = A()
AInst.set_x(9)
print(AInst.x)
BInst = B()
BInst.set_x(4)
print(BInst.x)

Output:

9
4

No fundamental difference between the two ways of attaching a method to a
class.

1

rocksolid light 0.9.8
clearnet tor