Re: Python getters and setters

2013-08-17 Thread Fernando Saldanha
The debate got more interesting than I expected. Without taking sides, I would like to add that perhaps my "length" example was misleading: length is easy to calculate. The information could be hard to extract from the data, either because complex calculations are involved, or because it is not

Re: Python getters and setters

2013-08-17 Thread Chris Angelico
On Sun, Aug 18, 2013 at 12:40 AM, Steven D'Aprano wrote: > The Law of Demeter is really about being careful about what interface > your classes provide. Customers should provide a get_payment method; dogs > should provide a walk method. You shouldn't have to send individual step > messages to the

Re: Python getters and setters

2013-08-17 Thread Steven D'Aprano
On Sun, 18 Aug 2013 00:52:06 +0200, Irmen de Jong wrote: > On 17-8-2013 19:18, Steven D'Aprano wrote: [...] >> How is this: >> >> obj.data_length() >> >> easier for the user than this? >> >> len(obj.data) >> >> > It's not easier for the user perse, but it might be preferable from a > design p

Re: Python getters and setters

2013-08-17 Thread Irmen de Jong
On 17-8-2013 19:18, Steven D'Aprano wrote: > On Sat, 17 Aug 2013 09:53:07 -0700, Fernando Saldanha wrote: > >> Suppose my class contains an attribute called "data" that can >> potentially provide a lot of information that will be needed by class >> users. I have two options: >> >> 1) For each piec

Re: Python getters and setters

2013-08-17 Thread Tim Chase
On 2013-08-17 17:18, Steven D'Aprano wrote: > # Yes, this is good, consistent design > len(myrecord.field) > len(obj.data) > len(data.value) > len(collection[key]) I would also add that, if the primary goal of your class is to encapsulate the data, you can do class MyClass: def __init__(sel

Re: Python getters and setters

2013-08-17 Thread Steven D'Aprano
On Sat, 17 Aug 2013 09:53:07 -0700, Fernando Saldanha wrote: > Suppose my class contains an attribute called "data" that can > potentially provide a lot of information that will be needed by class > users. I have two options: > > 1) For each piece of information within data (e.g., length) I write

Re: Python getters and setters

2013-08-17 Thread MRAB
On 17/08/2013 17:53, Fernando Saldanha wrote: I am new to Python. I understand that it is "unpythonic" to write getters and setters, and that property() can be used if necessary. This deals with the case of attributes, but there are other kinds of information available within a class. Suppos