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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:54:12+00:00 2026-05-27T11:54:12+00:00

I’m new to Python so forgive me if I’m not even using the right

  • 0

I’m new to Python so forgive me if I’m not even using the right terminology… I’m using Python 3.2 and I’m trying to figure out whether I can decorate a class property with some declarative-style information.

In my mind it would look like this:

class MyTestClass:

    def __init__(self, foo):
        self.foo = foo

    @property
    @somedeclarativeInfo("ABC",123)
    def radius(self):
        return self.__foo

    @radius.setter
    def radius(self, foo):
        self.__foo = foo

There are then two different things I’d want to do with the class:

A – Be able to interact with the foo property just like any other property (simple gets and sets)

B – Be able to dynamically find properties on a particular class that are decorated with this descriptor and be able to pull out the “ABC” and 123 values, etc.

I think maybe I should be creating a descriptor to accomplish what I want, but I’m not sure if I’m on the right track, or if this can be done.

Since my background is .Net I whipped up the following example to show what I want to do, in case that helps anyone understand my goal:

using System;
using System.Reflection;

namespace SampleWithProperties
{
    public class MyCustomAttribute : Attribute
    {
        public string Val1;
        public string Val2;

        public MyCustomAttribute(string val1,string val2)
        {
            Val2 = val2;
            Val1 = val1;
        }
    }

    public class Foo
    {
        [MyCustomAttribute("abc","def")]
        public string PropertyA { get; set; }

        [MyCustomAttribute("xyz","X")]
        public int PropertyB { get; set; }

        public string PropertyC { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Show that we can figure out which properties have the custom attribute,
            // and that we can get the values for Val1 and Val2

            foreach(PropertyInfo propertyInfo in typeof(Foo).GetProperties())
            {
                Console.WriteLine("Found a property named "+propertyInfo.Name);

                foreach(Attribute attribute in propertyInfo.GetCustomAttributes(
                    attributeType:typeof(MyCustomAttribute),inherit:true))
                {
                    Console.WriteLine("Found a MyCustomAttribute on the property.");

                    MyCustomAttribute myCustomAttribute = attribute as MyCustomAttribute;

                    Console.WriteLine("Val1 = " + myCustomAttribute.Val1);
                    Console.WriteLine("Val2 = " + myCustomAttribute.Val2);
                }

                Console.WriteLine();
            }

            // Show that the properties can be used like normal

            Foo foo = new Foo {PropertyA = "X", PropertyB = 2, PropertyC = "Z"};
            Console.WriteLine("Created an instance of Foo just for fun.  Its property values are "+
                foo.PropertyA+","+foo.PropertyB+","+foo.PropertyC);
        }
    }
}

Can this be done?

  • 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-27T11:54:13+00:00Added an answer on May 27, 2026 at 11:54 am

    OK, I wrote this question when I was first getting started with Python. I now know how to do in Python exactly what the .Net sample code I posted did. Granted, the biggest thing I didn’t realize when I originally posted the question was that descriptors alter the behavior of your attributes/properties(whatever you call them). Nonetheless, we can still allow these attributes to act like properties (and not change their behavior) yet put some metadata on them with the decorator. I’m currently implementing some protocol serialization/deserialization stuff where this is going to come in handy.

    class MyCustomDescriptor:
    
        def __init__(self,val1,val2):d
            self._val1 = val1
            self._val2 = val2
    
        @property
        def val1(self): return self._val1
    
        @property
        def val2(self): return self._val2
    
        def __call__(self,decorated_method_reference):
            self._decorated_method_reference = decorated_method_reference
            return self
    
        def __get__(self,instance,type=None):
    
            if not instance:
                return self
    
            return self._decorated_method_reference(instance)
    
    class Foo:
    
        def __init__(self,attribute_a_value,attribute_b_value,attribute_c_value):
            self._attribute_a_value = attribute_a_value
            self._attribute_b_value = attribute_b_value
            self._attribute_c_value = attribute_c_value
    
        @MyCustomDescriptor(val1="abc",val2="def")
        def attribute_a(self): return self._attribute_a_value
    
        @MyCustomDescriptor(val1="xyz",val2="X")
        def attribute_b(self): return self._attribute_b_value
    
        @property
        def attribute_c(self): return self._attribute_c_value
    
    # Show that by inspecting class Foo we can figure out which attribute are marked with MyCustomDescriptor and that
    # we can get the values for val1 and val2.  We don't even need an instance of Foo to do this.  The class itself is sufficient.
    
    print("Inspecting class Foo.  Looking for attributes marked with MyCustomDescriptor...")
    
    for attribute_name in dir(Foo):
    
        attribute_as_object = getattr(Foo,attribute_name)
    
        if type(attribute_as_object) == MyCustomDescriptor:
            print("attribute "+attribute_name+" is decorated with MyCustomDescriptor.  val1="+attribute_as_object.val1+" val2="+attribute_as_object.val2)
    
    # Show that the properties on Foo work like normal properties.  Note that I skipped implementing setters but could have done so.
    
    foo_instance = Foo(attribute_a_value="X",attribute_b_value=2,attribute_c_value="Z")
    
    print("Created an instance of Foo just for fun.  It's property values are "+str(foo_instance.attribute_a)+", "+str(foo_instance.attribute_b)+", "+str(foo_instance.attribute_c))
    

    The output is:

    Inspecting class Foo.  
    Looking for attributes marked with MyCustomDescriptor...
    attribute attribute_a is decorated with MyCustomDescriptor.  
    val1=abc val2=def
    attribute attribute_b is decorated with MyCustomDescriptor.  
    val1=xyz val2=X
    Created an instance of Foo just for fun.  
    It's property values are X, 2, Z
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.