Question on class member in python

J

Johnny Lee

Class A:
def __init__(self):
self.member = 1

def getMember(self):
return self.member

a = A()

So, is there any difference between a.member and a.getMember? thanks
for your help. :)

Regards,
Johnny
 
P

Peter Otten

Johnny said:
Class A:
def __init__(self):
self.member = 1

def getMember(self):
return self.member

a = A()

So, is there any difference between a.member and a.getMember? thanks
for your help. :)

Yes. accessor methods for simple attributes are a Javaism that should be
avoided in Python. You can always turn an attribute into a property if the
need arises to do some calculations behind the scene
.... def getMember(self):
.... return self.a * self.b
.... member = property(getMember)
.... def __init__(self):
.... self.a = self.b = 42
....1764

I. e. you are not trapped once you expose a simple attribute.

Peter
 
T

Tau

Get answer by typing:
id(a.member)==id(a.getMember())

You will often find id() useful when in doubt whether the two objects
are distinct.
 
B

bruno modulix

Johnny said:
Class A:
s/C/c/

def __init__(self):
self.member = 1

def getMember(self):
return self.member

a = A()

So, is there any difference between a.member and a.getMember?

yes : a.member is an integer, a.getMember is a bound method. You could
have found this by yourself...

Note that the getter/setter plague is useless and unpythonic. Use
properties instead if you need to control attributes access. There are
decorator idioms to make clean and easy properties, look here:
http://wiki.python.org/moin/PythonDecoratorLibrary
 
J

Johnny Lee

Peter Otten 写é“:
Yes. accessor methods for simple attributes are a Javaism that should be
avoided in Python. You can always turn an attribute into a property if the
need arises to do some calculations behind the scene

... def getMember(self):
... return self.a * self.b
... member = property(getMember)
... def __init__(self):
... self.a = self.b = 42
...
1764

I. e. you are not trapped once you expose a simple attribute.

Peter

Thanks for your help, maybe I should learn how to turn an attibute into
a property first.
 
J

Johnny Lee

But I still wonder what's the difference between the A().getMember and
A().member besides the style
 
A

Alex Martelli

Johnny Lee said:
Thanks for your help, maybe I should learn how to turn an attibute into
a property first.

Easy -- in your class's body, just code:

def getFoo(self): ...
def setFoo(self, value): ...
def delFoo(self): ...
foo = property(getFoo, setFoo, delFoo, 'this is the foo')


Note that if you want subclasses to be able to customize behavior of foo
accesses by simple method overrides, you need to program some "hooks"
(an extra level of indirection, if you will).

Alex
 
A

Alex Martelli

Johnny Lee said:
But I still wonder what's the difference between the A().getMember and
A().member besides the style

Without parentheses after it, getMember is a method. The difference
between a method object and an integer object (which is what member
itself is in your example) are many indeed, so your question is very
strange. You cannot call an integer, you cannot divide methods, etc.


Alex
 
J

Johnny Lee

Alex Martelli 写é“:
Without parentheses after it, getMember is a method. The difference
between a method object and an integer object (which is what member
itself is in your example) are many indeed, so your question is very
strange. You cannot call an integer, you cannot divide methods, etc.


Alex

Sorry, I didn't express myself clear to you. I mean:
b = A().getMember()
c = A().member
what's the difference between b and c? If they are the same, what's the
difference in the two way to get the value besides the style.
 
C

Christophe

Johnny Lee a écrit :
Alex Martelli 写é“:




Sorry, I didn't express myself clear to you. I mean:
b = A().getMember()
c = A().member
what's the difference between b and c? If they are the same, what's the
difference in the two way to get the value besides the style.

Second form is better because it makes it easier to refactor.

Like, at first A had a direct access to the member member. But later you
want to control the access to it. So you change it into a property and
you don't need to change client code.
 
A

Alex Martelli

Johnny Lee said:
Alex Martelli ???

Now that's a peculiar question...

Sorry, I didn't express myself clear to you. I mean:
b = A().getMember()
c = A().member
what's the difference between b and c? If they are the same, what's the
difference in the two way to get the value besides the style.

If getMember's body is nothing but a 'return self.member', then there is
no difference -- 'assert b is c'.

What is the difference between:

x = 2

and

y = 2+2-2*2/2

??? Answer: in terms of final results, no difference. On the other
hand, the second approach does a lot of obviously useless and intricate
computation, so it's a sheer waste of time and effort.

Exactly the same answer applies to your question -- obtaining the
..member attribute "indirectly", by calling a method that returns it,
does some obviously useless and moderately intricate computation, which
in some ways is a waste of (some) time and effort. That's all!


Alex
 
A

Aahz

Easy -- in your class's body, just code:

def getFoo(self): ...
def setFoo(self, value): ...
def delFoo(self): ...
foo = property(getFoo, setFoo, delFoo, 'this is the foo')


Note that if you want subclasses to be able to customize behavior of foo
accesses by simple method overrides, you need to program some "hooks"
(an extra level of indirection, if you will).

Or wait for

foo = property('getFoo', 'setFoo')

(Currently proposed by Guido on python-dev)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,269
Messages
2,571,348
Members
48,026
Latest member
ArnulfoCat

Latest Threads

Top