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

to property or function in class object

P: n/a
hi,

i am very new to python, not knowing much about good design. I have an
object here for example a Customer object, where I need to retrieve a
info which has a number of lines of code to get it.

my question is weather what approach should I use? to use the property
which is from the python new class style as I understand or simple use
function that will return the the info I needed.

class Customer(object):

current_balance = property(fget=_get_current_balance)

def _get_current_balance(self):
# coding here

or

def get_current_balance(self):
# coding here

While both approach would get the job done, I don't know much about
the performance, benefits, design philosophy between the two approach.
Any lecture will be very appreciated.

Thanks
james

Aug 17 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Fri, 17 Aug 2007 02:29:47 +0000, james_027 wrote:
i am very new to python, not knowing much about good design. I have an
object here for example a Customer object, where I need to retrieve a
info which has a number of lines of code to get it.

my question is weather what approach should I use? to use the property
which is from the python new class style as I understand or simple use
function that will return the the info I needed.
I'd say it's a matter of taste here. Where `get_*` and `set_*` methods
should not be used is getting and setting simple attributes. Here
properties are very handy if such a simple attribute changes to something
calculated later on, because it is possible to change the class without
the need to change the code that uses such objects.

In the other case, more complex code to get or set some information in
the first place, it is a matter of taste IMHO. Ask yourself if the user
would expect `balance` to be an attribute of that class. If it seems to
be "natural" to have such an attribute. And usually attribute access does
not trigger really heavy computation. Constructing the answer from some
different attributes or doing a conversion before returning something is
okay, but a calculation that lasts an hour or so would surprise many.

Ciao,
Marc 'BlackJack' Rintsch
Aug 17 '07 #2

P: n/a
james_027 wrote:
hi,

i am very new to python, not knowing much about good design. I have an
object here for example a Customer object, where I need to retrieve a
info which has a number of lines of code to get it.

my question is weather what approach should I use? to use the property
which is from the python new class style as I understand or simple use
function that will return the the info I needed.

class Customer(object):

current_balance = property(fget=_get_current_balance)
This will need to go down below the definition of _get_current_balance()
if you want to avoid an error in compilation.
def _get_current_balance(self):
# coding here

or

def get_current_balance(self):
# coding here

While both approach would get the job done, I don't know much about
the performance, benefits, design philosophy between the two approach.
Any lecture will be very appreciated.
It's pretty much a matter of choice when the method to retrieve the
value doesn't take any arguments. If it *does* take arguments then you
can't use a property.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Aug 17 '07 #3

P: n/a
james_027 wrote:
i am very new to python, not knowing much about good design. I have an
object here for example a Customer object, where I need to retrieve a
info which has a number of lines of code to get it.

my question is weather what approach should I use? to use the property
which is from the python new class style as I understand or simple use
function that will return the the info I needed.
[snip]
While both approach would get the job done, I don't know much about
the performance, benefits, design philosophy between the two approach.
I tend to view property as only something you use for the purposes of
backwards compatibility. So if you started with something as a simple
attribute and you later realize that it needs to do some computation,
you can use properties to keep the same API.

However, if I know right off the bat that I need to do some computation
to return a result, then I'll always prefer a method. People expect a
method call to take some computation. People don't always expect that
from a simple attribute access. So I think it's more predictable for
your users if you use a method when there's any real work being done.

STeVe
Aug 17 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.