在 Python 中输入对象命名空间
Enter object namespace in Python
有没有办法进入对象的命名空间,以便我可以像使用全局方法一样使用它的方法?我正在考虑使用 with 语句的东西。
class Bar():
def methodA(self):
# do stuff
def methodB(self):
# do more stuff
def __enter__(self):
# somehow enter object namespace / transfer methods into global namespace
def __exit__(self, *args):
# exit object namespace / get rid of globalized methods
foo = Bar()
with foo:
methodA() # all works fine
methodB()
methodA() # throws an error
这只是一个想法,可能根本行不通。或者也许有一个没有 with 语句的解决方案。
您或许可以使用此处描述的技术:Insert variable into global namespace from within a function?
我想它将需要在 __enter__
和 __exit__
函数中进行一些簿记,以便在其自身之后进行清理。这不是真正的标准,所以我忽略了其他一些脚枪。
这回答了最初的问题,但我建议不要使用它。
类似于wKavey的建议方式。
但我不确定为什么要这样做。
我需要确保全局命名空间中没有变量 methodA
。
class Bar():
def __init__(self, value=5):
self.value = value
def methodA(self):
return self.value
def methodB(self):
return -self.value
def __enter__(self):
global methodA
global methodB
methodA = self.methodA
methodB = self.methodB
def __exit__(self, *args):
global methodA
del methodA
global methodB
del methodB
pass
foo = Bar()
with foo:
print(methodA()) # all works fine
print(methodB())
methodA() # throws an error
(哦。 正在做同样的事情,但语法略有不同,先在这里...)
我不推荐任何东西,但你可以这样做(如果全局命名空间中已经有一个 methodA
,你当然会遇到麻烦):
class Bar():
def methodA(self):
print("methodA called")
def methodB(self):
print("methodB called")
def __enter__(self):
g = globals()
g["methodA"] = self.methodA
g["methodB"] = self.methodB
def __exit__(self, *args):
g = globals()
del g["methodA"]
del g["methodB"]
foo = Bar()
with foo:
methodA() # all works fine
methodB()
有没有办法进入对象的命名空间,以便我可以像使用全局方法一样使用它的方法?我正在考虑使用 with 语句的东西。
class Bar():
def methodA(self):
# do stuff
def methodB(self):
# do more stuff
def __enter__(self):
# somehow enter object namespace / transfer methods into global namespace
def __exit__(self, *args):
# exit object namespace / get rid of globalized methods
foo = Bar()
with foo:
methodA() # all works fine
methodB()
methodA() # throws an error
这只是一个想法,可能根本行不通。或者也许有一个没有 with 语句的解决方案。
您或许可以使用此处描述的技术:Insert variable into global namespace from within a function?
我想它将需要在 __enter__
和 __exit__
函数中进行一些簿记,以便在其自身之后进行清理。这不是真正的标准,所以我忽略了其他一些脚枪。
这回答了最初的问题,但我建议不要使用它。
类似于wKavey的建议方式。
但我不确定为什么要这样做。
我需要确保全局命名空间中没有变量 methodA
。
class Bar():
def __init__(self, value=5):
self.value = value
def methodA(self):
return self.value
def methodB(self):
return -self.value
def __enter__(self):
global methodA
global methodB
methodA = self.methodA
methodB = self.methodB
def __exit__(self, *args):
global methodA
del methodA
global methodB
del methodB
pass
foo = Bar()
with foo:
print(methodA()) # all works fine
print(methodB())
methodA() # throws an error
(哦。
我不推荐任何东西,但你可以这样做(如果全局命名空间中已经有一个 methodA
,你当然会遇到麻烦):
class Bar():
def methodA(self):
print("methodA called")
def methodB(self):
print("methodB called")
def __enter__(self):
g = globals()
g["methodA"] = self.methodA
g["methodB"] = self.methodB
def __exit__(self, *args):
g = globals()
del g["methodA"]
del g["methodB"]
foo = Bar()
with foo:
methodA() # all works fine
methodB()