English 中文(简体)
进入全球时的模块 __setattr__和__getattr__
原标题:A module s __setattr__ and __getattr__ when accessing globals

__getattr__ and __setattr__ works well when i get or set an object s attributes using dot notation. How do i intercept getting and setting module global variables from inside the module?

I 分级模块类型:

from types import ModuleType
class WUserModule(ModuleType):
    def __init__(self, name):
        super().__init__(name)

    def __setattr__(self, name, value):
        if name in self.__dict__:
             super().__setattr__(name, value)
             return
        print( Set name {}, value {} .format(name, str(value)))

    def __getattr__(self, name):
        print( Get name {} .format(name))

a. 建立空置模块和载荷代码:

module = WUserModule( form_module )
with open( user_module.py ,  r , encoding= utf8 ) as f:
    exec(f.read(), module.__dict__)

编码载于<代码>用户_module.py:

a=1
print(a)

我需要一些办法来拦截可变的<代码>a。 在装货代码中,预期能够使用模块<编码>全球(<>)中不存在的某些变量,并且希望替代所要求的数值。

最新资料:

以上代码按需要列出的t work:a <>>/code>的检索没有反映在上。

我正在利用PyQt4撰写平台,其他用户(方案者)在平台上添加处理与这些表格互动的形式和单元。 表格本身可通过投放变量form进入用户模块。 我想让用户能够以更简单的方式从植被的形式获取价值。 相反,没有写作if form.myCheck Box.isChecked()form.myCheck Box.setChecked(myValue) i 想提供一条捷径:if myCheck Box and myCheck Box = myValue ,拦截价值获取,并在以下背景下进行必要的工作:

def __getattr__(self, name):
    formWidget = getatttr(form, name)
    if isinstance(formWidget, QLineEdit):
        formWidget.setText(value)
...
最佳回答

你们能够这样做。

基本问题是,只有在你获得归属时,才赋予超负荷工作,具体地说,这意味着:

expr . ident

没有警棍,就不会有超负荷的特性。 因此,无论你做什么,顺序都像你那样。

a

不得援引超载的属性,而不论其他类型如何,均可等同于a

问题回答

在一般情况下,你试图做些什么。 你们既能有价值又能有属性。 您可以做以下工作:form.myCheck Box = real bool(form.myCheck Box)form.myCheck Box.setChecked(True)bool(form.myCheck Box.isChecked(),。 页: 1 t 可在TrueQCheck Box 反对的同时提交。 您可在<条码>bool、<条码>、>int和<条码>上查询。 处理方式虽不方便,但通常无法处理其他类型的问题。

除此之外,你还混淆了<代码>__getattr__。 本条并不要求具备的属性,getattribute__。 但是,使用<代码>__getattribute__是复杂的,你可能希望把检查箱作为描述器或使用特性,但你不得不放弃模块和使用类别。

P.S. Why is form a module, and not an instance of some object?





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签