method names nounVerb or verbNoun

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Feb 5 17:45:51 EST 2010


On Fri, 05 Feb 2010 12:26:38 -0800, Chris Rebert wrote:

> On Fri, Feb 5, 2010 at 11:53 AM, Wanderer <wanderer at dialup4less.com>
> wrote:
>> Which is the more accepted way to compose method names nounVerb or
>> verbNoun?
>>
>> For example voltageGet or getVoltage? getVoltage sounds more normal,
>> but voltageGet is more like voltage.Get. I seem to mix them and I
>> should probably pick one way and stick with it.
> 
> Use properties[1] and just call it `voltage`. Python is not Java [2];
> explicit getters/setters are unpythonic.


But sometimes you still need methods or functions of the form verb_noun. 
For instance, if getting or setting the voltage is an expensive 
operation, you don't want to fool the user into thinking it is a cheap 
attribute access. Or get_voltage might be a stand-alone function rather 
than a method. Or the getter might allow additional arguments. 

The admonition to avoid getters and setters isn't meant to prohibit *any* 
method which has a get/set in the name (whether implicit or explicit). 
It's meant as an antidote to the Java idiom of making *every* attribute 
private and accessing them via getters/setters, even when all they do is 
merely get/set the contents of the attribute.



-- 
Steven



More information about the Python-list mailing list