473,231 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

to property or function in class object

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
3 1099
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
0
by: Brian Young | last post by:
Hi all. I'm using the Property Grid control in a control to manage a windows service we have developed here. The windows service runs a set of other jobs that need to be managed. The control...
2
by: Lance | last post by:
I want to be able to reset a complex property in a PropertyGrid. I know that for properties that are ValueTypes you can include System.ComponentModel.DefaultValue in the declaration of the property....
0
by: Jordan Bowness | last post by:
I make a similar post in another newsgroup, but this example is simplified somewhat. I have a component (cmpMyComponent) with 2 properties. The 1st property is a string value (Description) and...
3
by: WStoreyII | last post by:
I have a Collection Class That I am Creating , In This Collection Class I have a default Property Item. My Problem is this I have about twenty different Criterias that can be used as a value...
6
by: Altman | last post by:
I would like to use an indexed property to access my private array of user controls. If I don't use an indexed property, the property will show up in my Properties Window during development. Is...
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
2
by: Lespaul36 | last post by:
I have a control that I have made that uses a custom class for storage of some of the information. I want it to display in the VS.Net 2003 property grid in the IDE like the Font property does, so...
3
by: bearophileHUGS | last post by:
Probably you have already discussed this topic, but maybe you can stand touching it again briefly. Maybe properties aren't used so often to deserve a specific syntax, but I don't like their syntax...
0
by: Hans Koller | last post by:
Hello group, I design a class to bind it to a property grid for easy modification of some settings. My problem is now that I want to raise an event when a settings has been changed. Thats not a...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.