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 7607777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:45:05+00:00 2026-05-31T00:45:05+00:00

I have some questions regarding the following code: 1 class Test(object): 2 def __init__(self):

  • 0

I have some questions regarding the following code:

  1 class Test(object):
  2     def __init__(self):
  3         print "Object instance created."
  4         self._x = raw_input("Initial value of x = ")
  5         print "Initial value of x set."
  6 
  7     def Property(func):
  8         return property(**func())
  9 
 10     @Property
 11     def x():
 12         def fget(self):
 13             print 'Getting x'
 14             return self._x
 15         def fset(self, val):
 16             print 'Setting x'
 17             self._x = val
 18         def fdel(self):
 19             print 'Deleting x'
 20             del self._x
 21         doc = "A test case"
 22         return locals()
  1. Why is the Property() function necessary?
  2. Why can’t I just return locals() and then use @property as a decorator directly?

When I do that I get an error saying x takes no arguments, one given (presumably ‘self’). I know python has the @x.setter option, however I’m forced to use 2.4 regularly, so it’s not an option for me. Even then, @x.setter still seems less elegant than defining it all in one block.

Is there a way to define it all in one block using @property?

  • 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-31T00:45:06+00:00Added an answer on May 31, 2026 at 12:45 am

    You can’t use property as a decorator directly for the code you have posted because it was not designed to be used that way, and it won’t work.

    If used as a decorator, property converts the function into the getter; if used as a function, you can pass in the getter, setter, deleter, and a doc.

    locals() returns all the locals, so you would have a dictionary with fget, fset, fdel, doc, Property, and __init__ — causing property to blow up because it was passed too many arguments.

    Personally, I like the @x.setter and @x.deleter style, as I don’t end up with unnecessary function names in the class name space.

    If you have to use 2.4 regularly, just roll your own (or steal the latest from 2.6 like I did ;):

        class property(object):
            "2.6 properties for 2.5-"    
            def __init__(self, fget=None, fset=None, fdel=None, doc=None):
                self.fget = fget
                self.fset = fset
                self.fdel = fdel
                self.__doc__ = doc or fget.__doc__
            def __call__(self, func):
                self.fget = func
                if not self.__doc__:
                    self.__doc__ = fget.__doc__
            def __get__(self, obj, objtype=None):
                if obj is None:
                    return self         
                if self.fget is None:
                    raise AttributeError("unreadable attribute")
                return self.fget(obj)
            def __set__(self, obj, value):
                if self.fset is None:
                    raise AttributeError("can't set attribute")
                self.fset(obj, value)
            def __delete__(self, obj):
                if self.fdel is None:
                    raise AttributeError("can't delete attribute")
                self.fdel(obj)
            def setter(self, func):
                self.fset = func
                return self
            def deleter(self, func):
                self.fdel = func
                return self
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There have been some similar questions asked regarding Grid views, but none have been
I have couple questions regarding some C++ rules. Why am I able to call
I came across this class while reading a C# book and have some questions.
I have the following code: http://jsfiddle.net/fCWJ5/1/ , and following doubts regarding the viewbox. body{margin:0;}
Writing some classes for a Framework extension, and I have the following code: public
I am reading some C++ text and got the following code: class A {
Quick question regarding EventHandlers in C#, let's say we have the following code: MyObject.MyEventHandler
Current I have some Views linked to ViewModels using code similar to the following:
I have several questions regarding code contracts, and the best practices for their usage.
My question is regarding JavaScript Closures and the Eval() function. I have some code

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.