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

  • SEARCH
  • Home
  • 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 789863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:35:30+00:00 2026-05-14T21:35:30+00:00

I am looking for a way to define properties in Python similar to C#,

  • 0

I am looking for a way to define properties in Python similar to C#, with nested get/set definitions.
This is how far I got:

#### definition ####    

def Prop(fcn):
    f = fcn()
    return property(f['get'], f['set'])


#### test ####

class Example(object):

    @Prop
    def myattr():

        def get(self):
            return self._value

        def set(self, value):
            self._value = value

        return locals()  #  <- how to get rid of this?

e = Example()
e.myattr = 'somevalue' 
print e.myattr

The problem with this is, that it still needs the definition to ‘return locals()’.
Is there a way to get rid of it?
Maybe with a nested decorator?

  • 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-05-14T21:35:31+00:00Added an answer on May 14, 2026 at 9:35 pm

    You could return get, set (a much more elegant approach) and make your Prop into

    def Prop(fcn):
        g, s = fcn()
        return property(g, s)
    

    There is however no clean way to not require any return statement in the decorated function. A function with internal def statements, just like one with internal assignments, does not actually execute those statements until it gets called — the objects and names said assignments and defs are supposed to build and bind are, literally, nowhere to be found.

    Once it is called, said names and objects are local to the function — so, they go away unless external references to them exist… and there’s really no more elegant way to ensure such external references to local names exist, besides returning them in some form.

    The problem comes from insisting that you want to decorate a function object (which keeps its local names very much to itself, by design). Everything would be fine and dandy if you agreed to use the correct keyword instead of def for the decorated thingy — that correct keyword is class. (Note, you need Python 2.6 or better for this purpose)…:

    def Prop(cls):
        f = cls.__dict__
        return property(f['get'], f['set'])
    
    
    #### test ####
    
    class Example(object):
    
        @Prop
        class myattr():
    
            def get(self):
                print 'getting', self._value
                return self._value
    
            def set(self, value):
                print 'setting', value
                self._value = value
    
    
    e = Example()
    e.myattr = 'somevalue' 
    print e.myattr
    

    Classes are much less secretive than functions wrt what’s “inside” them, so a class decorator can easily accomplish what you’re after. Note the tiny changes: __dict__ to access the dict of the class being decorated, s/def/class/ in the object being decorated, and removal of the return statement you dislike.

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

Sidebar

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.