Module level descriptors or properties

Discussion in 'Python' started by Floris Bruynooghe, Aug 21, 2007.

  1. Hi

    When in a new-style class you can easily transform attributes into
    descriptors using the property() builtin. However there seems to be
    no way to achieve something similar on the module level, i.e. if
    there's a "version" attribute on the module, the only way to change
    that to some computation later is by using a getter from the start as
    your public API. This seems ugly to me.

    Does anyone know of a better way to handle this?


    Regards
    Floris
    Floris Bruynooghe, Aug 21, 2007
    #1
    1. Advertising

  2. Floris Bruynooghe wrote:

    > Hi
    >
    > When in a new-style class you can easily transform attributes into
    > descriptors using the property() builtin. However there seems to be
    > no way to achieve something similar on the module level, i.e. if
    > there's a "version" attribute on the module, the only way to change
    > that to some computation later is by using a getter from the start as
    > your public API. This seems ugly to me.
    >
    > Does anyone know of a better way to handle this?


    Not really. All you can do is proxy all the calls through an actual object,
    most probably a singleton.

    Diez
    Diez B. Roggisch, Aug 21, 2007
    #2
    1. Advertising

  3. Floris Bruynooghe wrote:
    > When in a new-style class you can easily transform attributes into
    > descriptors using the property() builtin. However there seems to be
    > no way to achieve something similar on the module level, i.e. if
    > there's a "version" attribute on the module, the only way to change
    > that to some computation later is by using a getter from the start as
    > your public API. This seems ugly to me.
    >
    > Does anyone know of a better way to handle this?


    Better is of course subjective, but you can always do something like::


    class ModuleWrapper(...):
    def __init__(self, module):
    ...
    ...
    x = property(...)

    sys.modules[__name__] = ModuleWrapper(sys.modules[__name__])

    STeVe
    Steven Bethard, Aug 21, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Antoon Pardon
    Replies:
    14
    Views:
    449
    Steve Holden
    Mar 1, 2005
  2. Mudcat
    Replies:
    3
    Views:
    941
    Steven D'Aprano
    Jan 14, 2006
  3. Replies:
    3
    Views:
    332
    Bruno Desthuilliers
    Apr 21, 2006
  4. pabbu
    Replies:
    8
    Views:
    720
    Marc Boyer
    Nov 7, 2005
  5. Dmitry A. Soshnikov
    Replies:
    12
    Views:
    181
    Dr J R Stockton
    Apr 30, 2010
Loading...

Share This Page