Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何在Python中将属性方法作为Arumings传递 首先,我们考虑变量x/p>的get和set方法来考虑这个工作示例。 class Foo: def __init__(self): self._x = 0 def set_x(self, x): self._x = x def get_x(self): return self._x class Bar: def __init__(self, set_method): self._set_method = set_method def set_x(self, x): self._set_method(x) f = Foo() f.set_x(5) print(f.get_x()) # Prints 5 b = Bar(f.set_x) b.set_x(10) print(f.get_x()) # Prints 10_Python 3.x_Methods_Properties - Fatal编程技术网

Python 3.x 如何在Python中将属性方法作为Arumings传递 首先,我们考虑变量x/p>的get和set方法来考虑这个工作示例。 class Foo: def __init__(self): self._x = 0 def set_x(self, x): self._x = x def get_x(self): return self._x class Bar: def __init__(self, set_method): self._set_method = set_method def set_x(self, x): self._set_method(x) f = Foo() f.set_x(5) print(f.get_x()) # Prints 5 b = Bar(f.set_x) b.set_x(10) print(f.get_x()) # Prints 10

Python 3.x 如何在Python中将属性方法作为Arumings传递 首先,我们考虑变量x/p>的get和set方法来考虑这个工作示例。 class Foo: def __init__(self): self._x = 0 def set_x(self, x): self._x = x def get_x(self): return self._x class Bar: def __init__(self, set_method): self._set_method = set_method def set_x(self, x): self._set_method(x) f = Foo() f.set_x(5) print(f.get_x()) # Prints 5 b = Bar(f.set_x) b.set_x(10) print(f.get_x()) # Prints 10,python-3.x,methods,properties,Python 3.x,Methods,Properties,如您所见,我将设置类Foo的实例f的变量x的可能性传递给类Bar的实例b 现在,我想做同样的事情,但是用房地产装饰来代替,大致像这样 class Foo: def __init__(self): self._x = 0 @property def x(self): return self._x @x.setter def x(self, x): self._x = x class Bar: def

如您所见,我将设置类Foo的实例f的变量x的可能性传递给类Bar的实例b

现在,我想做同样的事情,但是用房地产装饰来代替,大致像这样

class Foo:
    def __init__(self):
        self._x = 0
    @property
    def x(self):
        return self._x

    @x.setter
    def x(self, x):
        self._x = x

class Bar:
    def __init__(self, x_property):
        self._x_property = x_property

    def set_x(self, x):
        self.x_property = x

f = Foo()
f.x = 5
print(f.x)
# Prints 5
b = Bar(f.x)
b.set_x(10)
print(f.x)
# Prints 5
发生的情况是,将值5而不是属性传递给实例b,这意味着b无法访问实例f中的x。有什么好办法解决这个问题吗

然后我还想对get方法做同样的事情。在第一个代码中,需要我同时传递这两个方法,但如果有办法让第二个代码工作,我希望只需要传递属性,然后我就可以将其设置为普通变量并获取

我真的很想使用属性装饰器或类似的工具,因为它会大量清理我的代码。我使用python 3.5.2

谢谢,
Andreas

您可以通过访问
Foo.x
fset
属性来实现这一点。注意使用类点符号而不是实例点
fset
接受两个参数:要访问的实例和要写入的值。下面是一个工作示例

class Foo:
    @property
    def x(self):
        return self._x

    @x.setter
    def x(self, x):
        self._x = x

class Bar:
    def __init__(self, x_property):
        self.x_property = x_property

    def set_x(self, foo, value):
        self.x_property(foo, value)

f = Foo()
f.x = 5
print(f.x)
b = Bar(Foo.x.fset)
b.set_x(f, 10)
print(f.x)
注意,我们必须将
f
传递给
set\ux
,因为我们需要它来调用setter。通过使用
functools
模块中的
partial
f
绑定到属性设置器,可以消除
f
参数。将
partial
绑定传递给
Bar
的构造函数

class Bar:
    def __init__(self, x_property):
        self.x_property = x_property

    def set_x(self, value):
        self.x_property(value)

f = Foo()
b = Bar(partial(Foo.x.fset, f))
b.set_x(10)
print(f.x)

重命名
x_属性
和这一点可能是明智的。就
Bar
而言,它实际上只是一个函数。它不必是属性。

您可以通过访问
Foo.x
fset
属性来实现这一点。注意使用类点符号而不是实例点
fset
接受两个参数:要访问的实例和要写入的值。下面是一个工作示例

class Foo:
    @property
    def x(self):
        return self._x

    @x.setter
    def x(self, x):
        self._x = x

class Bar:
    def __init__(self, x_property):
        self.x_property = x_property

    def set_x(self, foo, value):
        self.x_property(foo, value)

f = Foo()
f.x = 5
print(f.x)
b = Bar(Foo.x.fset)
b.set_x(f, 10)
print(f.x)
注意,我们必须将
f
传递给
set\ux
,因为我们需要它来调用setter。通过使用
functools
模块中的
partial
f
绑定到属性设置器,可以消除
f
参数。将
partial
绑定传递给
Bar
的构造函数

class Bar:
    def __init__(self, x_property):
        self.x_property = x_property

    def set_x(self, value):
        self.x_property(value)

f = Foo()
b = Bar(partial(Foo.x.fset, f))
b.set_x(10)
print(f.x)

重命名
x_属性
和这一点可能是明智的。就
Bar
而言,它实际上只是一个函数。它不一定是一个财产。

总体而言,你想实现什么?总体而言,你想实现什么?