0

I have found previous discussing about defining Static Methods in (Iron)Python, however, I didn't find any about Static Properties. I assume you can definitively create Static Properties since properties are just methods for the .NET CLR and that's what I did in the code below, however, it looks like by calling the Static Property "StaticField" I cannot access the value of the Static Field "__staticField" it is linked to instead I get a reference where the property is stored? , but If I use the Static Method "getStaticField" that is used as the Get Property it does correctly give me the value "2".

So the question is: can you define Static Properties in (Iron)Python? and how can I use them to get the value and not the reference to the property method?

Thanks in advance.

class Test(object):
    __instanceField = 0
    __staticField = 0    

    # Instance Property (read-only)
    def getInstanceField(self):   
        return self.__instanceField    
    InstanceField = property(getInstanceField, None, None)

    # Static Property (read-only)
    @staticmethod
    def getStaticField():
        return Test.__staticField        
    StaticField = property(getStaticField, None, None)

    # Instance Method
    def instanceMethod(self, n):
        self.__instanceField += 1   
        print 'instanceMethod', n

    # Static Method
    @staticmethod
    def staticMethod(n):
        Test.__staticField += 1  
        print 'staticMethod', n

# Calling Static Methods
Test.staticMethod(5)
Test.staticMethod(10)

# Calling Instance Methods
t = Test()
t.instanceMethod(5)
t.instanceMethod(10)

print 'InstanceProperty', t.InstanceField 
#prints 2
print 'StaticProperty', Test.StaticField 
#prints: <property object at 0x000000000000002B>
print 'StaticPropertyMethod', Test.getStaticField()
#prints 2
1
  • I think I found what the problem "could" be... The problem seems to be that the StaticProperty = property(getStaticField, none, none) is getting the Getter parameter as an instance method, that's why when I call it later on it gives me the property object at 0x0000... The thing is that it doesn't allow me to assign a getter as Static: StaticField = property(Test.getStaticField, None, None) Error: global Test is not defined. Commented Jul 9, 2011 at 22:05

1 Answer 1

1

this answer is for python in general, and not specific to IronPython.

property is a convenience for creating a descriptor, an object which supplies a __get__ and optionally __set__ and __del__ methods. The __get__ method accepts arguments for the target instance and also the associated class, but __set__ is never called for classes. If all you need is getter behavior without setter behavior, just implement a descriptor directly

class StaticGetter(object):
    def __init__(self, attr):
        self.attr = attr
    def __get__(self, instance, owner):
        # owner is the class, your getter code here
        return getattr(owner, attr)

class Test(object):
    __staticField = 0
    staticField = StaticGetter('_Test__staticField')

In the case of a setter, you could supply a __set__ method, which infers the class from the instance, but arranging for Test.staticField = newvalue to instead call Test.staticField.someSetterMethod(newvalue) will require a new metaclass.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your comment TokenMacGuy, interesting use of the getattr function (or __getattribute__()) but I do not want to define a separate class just to handle an static property if I can do the same using the getStaticField() method instead. The real question here is if I can define a static property as a syntax built-in support in (iron)python.
you could supply your own __getattribute__ method in a metaclass to do this, but that solution would be a bit more convloluted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.