[Python-Dev] PEP 549: Instance Properties (aka: module properties) (original) (raw)

Victor Stinner victor.stinner at gmail.com
Mon Sep 11 13:20:44 EDT 2017


2017-09-11 19:00 GMT+02:00 Chris Barker <chris.barker at noaa.gov>:

I wish there were a property feature available almost very time I encounter a "get*" method in the stdlib (or any where):

There are a heck of a lot in the os module: In [4]: [s for s in dir(os) if s.startswith('get')] Out[4]: ['getblocking',

This one is not a good example: it takes a parameter. You cannot convert it to a property.

'getcwd',

And just yesterday I was getting annoyed by some in sysconfig:

In [6]: [s for s in dir(sysconfig) if s.startswith('get')] Out[6]: ['getconfighfilename', 'getconfigvar', 'getconfigvars', 'getmakefilefilename', 'getpath', 'getpathnames', 'getpaths', 'getplatform', 'getpythonversion', 'getschemenames']

When designing an API, when I have to choose between property and function/method, I prefer function/method over a property when the code is slow, especially at the first call.

I prefer to "warn" users than a call (like the first one which fills a cache) can be slow.

Well, it's not a strong rule, sometimes I use a property even if the first call has to fill a cache :-)

Here the sysconfig has to build an internal cache at the first call ... if I recall correctly.

Victor



More information about the Python-Dev mailing list