简体   繁体   English

在类中更改类变量引用

[英]Change class variable reference within the class

I want to be able to change the reference of a variable within the class Test 我希望能够在Test类中更改变量的引用

class Test():
    def change(self, Other_Class):
        self.__class__ = Other_Class.__class__
        self = Other
class Other():
    def set_data(self, data):
        self.data = data
    def one(self):
        print('foo')

a = Test()
b = Other()
b.set_data([1,2,3])
a.change(b)
a.data

AttributeError: 'Other' object has no attribute 'data'

How can I change the reference to a to be what ever variable I pass through to Test().change 如何将对a的引用更改为传递给Test()的变量

I would like this to work for builtin datatypes as well, but I get a different error for that. 我也希望此方法也适用于内置数据类型,但是为此我得到了另一个错误。

what would be the best way to do this? 最好的方法是什么?

Inside Test.change , that self is a parameter, and parameters are just local variables. Test.change内部, self是一个参数,而参数只是局部变量。

And rebinding local variables doesn't have any effect on anything outside of the function. 并且重新绑定局部变量对函数以外的任何内容都没有任何影响。

In particular, it has no effect on any other variables (or list elements, or attributes of other objects, etc.), like the global a , that were also bound to the same value. 特别是,它对绑定到相同值的任何其他变量(或列表元素或其他对象的属性等)没有影响,例如全局a They remain names for that same value. 它们仍然是相同值的名称。

It's not even clear what you're trying to do here. 甚至不清楚您要这里做什么。 You change the type of a into the type of b , and that works. 您将a的类型更改为b的类型,并且可以使用。 But what else do you want to do? 但是您还想做什么?

Do you want to change a into the object b , with the same identity? 你想改变a到对象b ,具有相同的身份? If so, you don't need any methods for that; 如果是这样,则不需要任何方法。 that's what a = b means. 那就是a = b意思。 Or do you want to be a distinct instance, but share an instance __dict__ ? 还是您想成为一个独特的实例,但要共享一个实例__dict__ Or to copy all of b 's attributes into a ? 还是将b的所有属性复制到a Shallow or deep? 浅还是深? Should any extra attributes a had lying around be removed as well? 如果有任何额外的属性a已经躺在附近被同时删除? Do you only care about attributes stored in the __dict__ , or do you need, eg, __slots__ to work? 您是否只关心__dict__存储的属性,还是需要__slots__才能工作?

Anyway, something that might be reasonable, for some strange use case, is this: 无论如何,对于某些奇怪的用例, 可能是合理的事情是:

def change(self, other):
    inherited = dict(inspect.getmembers(self.__class__))
    for name, value in inspect.getmembers(self):
        if name not in inherited and not name.startswith('__'):
            delattr(self, name)
    self.__class__ = other.__class__
    inherited = dict(inspect.getmembers(other.__class__))
    for name, value in inspect.getmembers(other):
        if name not in inherited and not name.startswith('__'):
            setattr(self, name, value)

Whether that's useful for your use case, I have no idea. 这是否对您的用例有用,我不知道。 But maybe it gives you an idea of the kinds of things you can actually do with the Python data model. 但是也许它使您对使用Python数据模型可以实际执行的操作类型有所了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM