我想做的是像
$userID = getFields('users', JFactory::getUser(), true);
$db->setQuery("SELECT rep_id, inventory_id, value
FROM #data
WHERE inventory_id_id = 1
AND rep_id = " $userID ");
$results = $db->loadObjectList();
当然,我可以在其init函数中将class my_class:
def __init__(self, foo):
self.foo = foo
self.bar = attribute_class()
self.bar.some_function()
class attribute_class:
def __init__(self):
# This is where I don't know what I have to write
def some_function(self):
do_something_with(foo)
对象传递给my_class
对象,但这有点愚蠢。我也可以传递所需的属性,但是由于python不知道指针,因此将创建多余的信息。此外,“父母的”属性不会自动更改“孩子的”属性。
答案 0 :(得分:0)
Python函数通常处于闭包状态,即它们可以捕获上下文。 例如:
In [1]: class Foo:
...: def __init__(self, x):
...: self.data = x
...: def getter():
...: return self.data
...: def setter(y):
...: self.data = y
...: self.getter = getter
...: self.setter = setter
...:
In [2]: x = Foo(12)
In [3]: x.getter()
Out[3]: 12
In [4]: y = x.getter
In [5]: z = x.setter
In [6]: y()
Out[6]: 12
In [7]: z(42)
In [8]: y()
Out[8]: 42
请注意,这可能会帮助您了解要执行的操作,因为可能有更好的方法来代替将函数(闭包)作为属性...