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

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]:
>
> ['get_blocking',

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]:
>
> ['get_config_h_filename',
>  'get_config_var',
>  'get_config_vars',
>  'get_makefile_filename',
>  'get_path',
>  'get_path_names',
>  'get_paths',
>  'get_platform',
>  'get_python_version',
>  'get_scheme_names']

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