Sujet : Re: Difference method vs attribute = function (Posting On Python-List Prohibited)
De : ldo (at) *nospam* nz.invalid (Lawrence D'Oliveiro)
Groupes : comp.lang.pythonDate : 30. Jun 2024, 00:55:54
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v5q3dq$4pv6$2@dont-email.me>
References : 1 2
User-Agent : Pan/0.158 (Avdiivka; )
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.