Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #75755
| References | <CA+9L8FypV3+D1cMTca7Dmwju6UB_myDtMUGukpY1fGGZLcbLGQ@mail.gmail.com> |
|---|---|
| Date | 2014-08-06 06:56 +1000 |
| Subject | Re: Making every no-arg method a property? |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.12675.1407272179.18130.python-list@python.org> (permalink) |
On Wed, Aug 6, 2014 at 5:39 AM, Christian Calderon <calderon.christian760@gmail.com> wrote: > I have been using python for 4 years now, and I just started learning ruby. > I like that in ruby I don't have to type parenthesis at the end of each > function call if I don't need to provide extra arguments. I just realized > right now that I can do something similar in python, if I make all methods > with only the implicitly passed 'self' into properties. Which means I can > either do some fancy coding and make a metaclass that does this > auto-magically, or I have to have property decorators all over the place :-P > . I was wondering what other thought of this, is it an overly fanciful way > of coding python, or is it an acceptable thing to do in a real project? > Also, would anyone be interested in helping me make this metaclass? It's not a Pythonic style of coding. A property is generally assumed to be a cheap and simple lookup that cannot change anything - that is, it should be 'safe'. A no-arg method could mutate the object, could be expensive, could do just about anything. The parentheses make it clear that something's now happening - code's being called. Properties should be reserved for those cases where it's conceptually an attribute lookup, but for whatever reason you need a bit of code on it - and that's a pretty unusual case, compared to zero-argument methods. Strong recommendation that you don't do this. Of course, Python's "consenting adults" policy says that you're most welcome to - but you'll find that it's much more friendly to subsequent maintainers to keep attribute lookup and method call different. ChrisA
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Making every no-arg method a property? Chris Angelico <rosuav@gmail.com> - 2014-08-06 06:56 +1000
csiph-web