Can a python descriptor be used to instantiate an attribute in the __init__ of another class?

自作多情 提交于 2020-01-03 05:22:08

问题


Or does the attribute have to be defined outside of any class methods?

So my descriptor object is this. The IDN object already has some information about the UserNameField, so I want to use it.

class UserNameElement(basePageElement):
    _testMethodName="UserNameElement Test method"
    def __init__(self, IDN, PTF):
        print "creating UserNameElement"
        self.locator =  IDN.UserNameField()

And here is my calling class. Where I want to instantiate the UserNameElement object

class LoginPageObject(basePageObject):
    _testMethodName="LoginPageObject Test method"
    print "creating LoginPageObject"
    def __init__(self, BaseURL):
        super(LoginPageObject, self).__init__()
        self.username=UserNameElement(IDN=self.IDN, PTF=self.PTF)

It seems that the standard process would put the username= in in the general class definition, like this:

class LoginPageObject(basePageObject):
    _testMethodName="LoginPageObject Test   method"
    username=UserNameElement()
    print "creating LoginPageObject"
    def __init__(self, BaseURL):
        super(LoginPageObject, self).__init__()

But then I don't have the PTF and IDN that I define in the basePageObject class.

What can I do to make those available when the username attribute is created?

Thanks


回答1:


I am afraid that will not be possible, as your attribute username will be resolved via normal attribute access see http://docs.python.org/howto/descriptor.html#invoking-descriptors

May be you can get away by overriding __getattribute__ and simulating what type.__getattribute__() does

class MyD(object):
    def __init__(self, val):
        self.val = val

    def __get__(self, obj, objtype):
        return self.val

    def __set__(self, obj, val):
        self.val = val

class C(object):
    a = MyD(42)
    def __init__(self):
        self.d = MyD(42)

    def __getattribute__(self, name):
        attr = super(C, self).__getattribute__(name)
        if hasattr(attr, '__get__'):
            return attr.__get__(self, C)
        return attr

c = C()
print c.d
print c.a

Output:

42
42



回答2:


Since you probably won't need the username until the object has been instantiated, it's probably best to just make it a property and write a getter for it.



来源:https://stackoverflow.com/questions/10232174/can-a-python-descriptor-be-used-to-instantiate-an-attribute-in-the-init-of-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!