Does python support the C# notation of member decoration?

A forum for general discussion of the Python programming language.

Does python support the C# notation of member decoration?

Postby SubZero00 » Tue Apr 08, 2014 2:09 am

Hi all,

Does python support the C# notion of member decoration, eg:

Code: Select all
     class AClass:
          def __init__(self):
               [exportLocation=0]
               self.someVariable  = 5


Or some other way of associating attributes with member variables, or functions, and a way of accessing these attributes programmatically? This would be *extremely* useful to have in the language, even if it was necessary to specify the name of the member, eg: [someVariable, someAttribute=someData]

Thanks,
Paul
Last edited by Yoriz on Wed Apr 09, 2014 12:15 pm, edited 3 times in total.
Reason: First post lock, Changed Title
SubZero00
 
Posts: 2
Joined: Tue Apr 08, 2014 2:04 am

Re: Attribute decorations for members

Postby Mekire » Tue Apr 08, 2014 2:52 am

Not sure exactly what you are asking as I'm not versed in C# but python fully supports decorators.

It sounds like you are asking about something like the property decorator which allows you to treat a function like a standard attribute:
Code: Select all
import math


class Vector(object):
    def __init__(self, x,y):
        self.x = x
        self.y = y

    @property
    def length(self):
        return math.hypot(self.x, self.y)


if __name__ == "__main__":
    vec = Vector(5, 10)
    print(vec.length)
    vec.x += 20
    print(vec.length)

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Attribute decorations for members

Postby stranac » Tue Apr 08, 2014 9:40 am

If what Mekire posted doesn't help, try explaining the C# example.
It can probably be done in python, but most of us have no idea what that does.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1152
Joined: Thu Feb 07, 2013 3:42 pm

Re: Attribute decorations for members

Postby SubZero00 » Wed Apr 09, 2014 1:45 am

Hi all,

Thanks for the replies. Effectively, what you can do in C# is decorate a method, class, etc, with a set of attributes, similar to XML:

Code: Select all
        <someMember exportLocation=0>
                <someData />
        </someMember>

        <someMember exportLocation=1>
                <someData />
        </someMember>


What I would like to do in Python is to be able to retrieve attribute information associated with a member (Python attribute). For example, in the above example, there is a XML attribute "exportLocation" associated with the section "someMember". I would like to have similar "attribute" information associated with data in Python, for example:

Code: Select all
        class MyClass:
                def __init__(self):
                        [exportLocation=0]
                        self.someMember = someData


Using something similar to the above, I would like to be able to retrieve the "exportLocation" attribute from someMember through reflection at runtime. Specifying attribute data with methods or members is extremely useful, as for example, during serialisation of class data - I could specify an attribute such as "[serialise]" above a class member which would specify to an external class which was iterating over the "members" of the class that it should serialize the data. I could then further specify additional attribute information such as "exportLocation" which would specify the offset to serialise the data to.

An interface similar to the following would integrate fairly well into the existing runtime:

Code: Select all
        classMember = getattr(myClassInstance, "someMember")
        for memberAttribute in classMember.getAttributeList():
                print(memberAttribute.getAttributeName())


Which would then print out "exportLocation" as the name of the attribute from the above Python example. This would be extremely useful.

Thanks,
Paul
SubZero00
 
Posts: 2
Joined: Tue Apr 08, 2014 2:04 am

Re: Attribute decorations for members

Postby Kebap » Wed Apr 09, 2014 12:24 pm

Hi Paul!

Not sure if I understand your explanations right, but it seems to me you can just go like this:

Code: Select all
class MyClass:
    def __init__(self):
        self.serialise = True

myObject = myClass()
if myObject.serialise == True:
    # do stuff
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 396
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 3 guests