Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8339259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:50:12+00:00 2026-06-09T04:50:12+00:00

Is it possible to write a decorator that creates many properties at once? Like

  • 0

Is it possible to write a decorator that creates many properties at once?

Like instead of writing

class Test:
    @property
    def a(self):
        return self.ref.a
    @property
    def b(self):
        return self.ref.b

I’d like to write

class Test:
    @properties("a", "b")
    def prop(self, name):
        return getattr(self.ref, name)

Is it possible? Do you recommend it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:50:15+00:00Added an answer on June 9, 2026 at 4:50 am

    Recall that a decorator

    @decorator(dec_args)
    def foo(args):
        pass
    

    is just syntactic sugar for writing

    def foo(args):
        pass
    foo = decorator(dec_args)(foo)
    

    So it is not possible for a method decorator to result in more than one method (or property, etc.) to be added to a class.

    An alternative might be a class decorator that injects the properties:

    def multi_property(prop, *names):
        def inner(cls):
            for name in names:
                setattr(cls, name, property(lambda self, name=name: prop(self, name)))
        return inner
    
    @multi_property(prop, 'a', 'b')
    class Test:
        pass
    

    However it’d usually be clearer to have each property present within the body of the class:

    a = forward_property('ref', 'a')
    b = forward_property('ref', 'b')
    

    where forward_property returns a property object as appropriate implementing the descriptor protocol. This is friendlier to documentation and other static analysis tools, as well as (usually) the reader.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to write a doctest unit test that will check that an
Is it possible to write GLSL ES fragment shaders under iOS that generate multiple
Is it possible to write generic class with one constructor which explicitly defines type
Is it possible to write a program that will change the phone numbers a
I was wondering if i could possible write an app, that could be a
it's possible write something like: function foo(a,b,c) { return a + b + c;
Is it possible to write your own conditional statements or overload current ones? (Like
I want to write a python decorator to decorate a test function of a
Possible Duplicate: Write once deploy on Windows Mobile 6, Windows Phone 7, Android and
Possible Duplicate: Write a recursive function that reverses the input Recently, I've been reading

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.