method names nounVerb or verbNoun
clp2 at rebertia.com
Sat Feb 6 00:36:19 CET 2010
On Fri, Feb 5, 2010 at 2:45 PM, Steven D'Aprano
<steve at remove-this-cybersource.com.au> wrote:
> 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>
>>> Which is the more accepted way to compose method names nounVerb or
>>> 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 and just call it `voltage`. Python is not Java ;
>> explicit getters/setters are unpythonic.
> But sometimes you still need methods or functions of the form verb_noun.
True, but when the verb is "get" and you're using Python, your design
probably merits another going-over. If the OP hadn't used "get" as
their example, I would have answered significantly differently.
> 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.
I strongly disagree with this rationale as it violates the Uniform
(http://en.wikipedia.org/wiki/Uniform_access_principle). Though I
concede Python's builtins don't adhere to the UAP.
> Or get_voltage might be a stand-alone function rather
> than a method.
The OP originally said quite explicitly it was a method in their
original post. (Though they since wisely changed their code structure
so it's not.)
> Or the getter might allow additional arguments.
True; but before the OP posted the actual function body, there was no
way to know whether it did. Lacking such info, I assumed the simplest
and most frequent case, namely that it didn't take any args.
> 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.
True. Again, before the OP posted the method body, there was no way to
know it wasn't a trivial Java-esque getter.
More information about the Python-list