1

For example, in C# I can do the following using reflection:

public class A
{
    object obj1;

    [MyCustumAttribute(data)]
    public object Obj1Property
    { get; set; }

    public A() {}
}

public static void Main(string[] args)
{
    Type t = typeof(a);
    PropertyInfo[] props = t.GetProperties();
    attrs = props[0].GetCustomAttributes();

    //Do something with the properties based on their custom attributes
}

Note that I can do the same thing with A's methods, data members, etc. not just on properties.

Is there a way to do this same type of thing in Python 2.7?

Mike Webb
  • 8,855
  • 18
  • 78
  • 111
  • I did not. I'm learning that I sometimes need to rethink my solution entirely when doing something in another language. I'm obviously thinking in C# here. I will need to re-think what I was originally trying to accomplish and make it Pythonic. Thanks for the help. :) – Mike Webb Dec 26 '11 at 17:25

2 Answers2

0

EDIT: After re-reading your question I realized that the reflection aspect wasn't addressed. The answer is still no from what I can find though. .NET reflection doesn't seem to address DLR types (e.g. IronPython, IronRuby). There is a reflection mechanism in python that you may be able to utilize though, see https://stackoverflow.com/a/1447529/635634 for details.

-

Not really, but you can sort of achieve the same effect using DevHawk's solution.

Community
  • 1
  • 1
M.Babcock
  • 18,753
  • 6
  • 54
  • 84
0

Something that could help to accomplish what you are trying to achieve are python decorators (extensive info on decorators In this SO answer)

Briefly, they are available since version 2.4 and allow you to wrap or decorate a function - In order to determine if a given function has a decorator you could attempt to evaluate using the functools library.

Another way that could help you accomplish this since you want to decorate a class are Python MetaClasses

Community
  • 1
  • 1
Anastasiosyal
  • 6,494
  • 6
  • 34
  • 40