Python Desciptors

Having an issue with descriptors in Python. Are the __get__ and __set__ not supported?

Here is the code:

class tagger(object):
    def __init__(self, tagPath):
        self.tagPath = tagPath

    def __get__(self, obj, objtype):
    	print("get")
        return system.tag.read(self.tagPath).value

    def __set__(self, obj, val):
    	print("set")
        system.tag.write(self.tagPath, val)

    def __str__(self):
        return str(system.tag.read(self.tagPath).value)
        
class exeEngStepper(object):

    def __init__(self, exeEngStepID):

        self.exeStepID = exeEngStepID
        self.completeC = tagger("[default]ExecutionStepList/exeEngStep" + str(self.exeStepID) + "/completeC")
       
tc = exeEngStepper(1)
type(tc.completeC)
print(tc.completeC)
tc.completeC
tc.completeC = 12
type(tc.completeC)

Here is the output:

<class '__main__.tagger'>
111
<__main__.tagger object at 0x15>
<type 'int'>

Per the docs, those methods only work when owned by a parent class. Not free-form. I recommend you implement getattr and setattr on your exeEngStepper class itself, where the tagpaths are managed within exeEngStepper. Or use the @property decorators.

I can certainly try that. I must have miss understood the definition. I was following this How To and I thought it was basically the same.

No, you’re trying to use tagger as an instance variable (with self.), where that example shows the assignment to x at the class level, the same level as method definitions. (No access to the containing class’s instance variables.)

Ah that makes sense now. Thanks!