By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,621 Members | 1,068 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,621 IT Pros & Developers. It's quick & easy.

Question on class member in python

P: n/a
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

Oct 17 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Johnny Lee wrote:
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
class A(object): .... def getMember(self):
.... return self.a * self.b
.... member = property(getMember)
.... def __init__(self):
.... self.a = self.b = 42
.... A().member

1764

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

Peter
Oct 17 '05 #2

P: n/a
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.

Oct 17 '05 #3

P: n/a
Johnny Lee wrote:
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
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Oct 17 '05 #4

P: n/a

Peter Otten 写道:
Johnny Lee wrote:
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
class A(object): ... def getMember(self):
... return self.a * self.b
... member = property(getMember)
... def __init__(self):
... self.a = self.b = 42
... A().member

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.

Oct 18 '05 #5

P: n/a
But I still wonder what's the difference between the A().getMember and
A().member besides the style

Oct 18 '05 #6

P: n/a
Johnny Lee <jo************@hotmail.com> wrote:
...
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
Oct 18 '05 #7

P: n/a
Johnny Lee <jo************@hotmail.com> wrote:
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
Oct 18 '05 #8

P: n/a

Alex Martelli 写道:
Johnny Lee <jo************@hotmail.com> wrote:
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


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.

Oct 18 '05 #9

P: n/a
Johnny Lee a écrit :
Alex Martelli 写道:

Johnny Lee <jo************@hotmail.com> wrote:

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

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.
Oct 18 '05 #10

P: n/a
Johnny Lee <jo************@hotmail.com> wrote:
Alex Martelli ???


Now that's a peculiar question...

Johnny Lee <jo************@hotmail.com> wrote:
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


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
Oct 18 '05 #11

P: n/a
In article <1h***************************@yahoo.com>,
Alex Martelli <al*****@yahoo.com> wrote:
Johnny Lee <jo************@hotmail.com> wrote:
...
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).


Or wait for

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

(Currently proposed by Guido on python-dev)
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"If you think it's expensive to hire a professional to do the job, wait
until you hire an amateur." --Red Adair
Oct 18 '05 #12

P: n/a
It looks like there isn't a last word of the differrences....

Oct 21 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.