草庐IT

specify-an-init-process

全部标签

python - 如何编写元类以防止在 __init__() 之后创建新属性?

目前,我在类的__init__()方法末尾覆盖类的__setattr__()方法以防止创建新属性-classPoint(object):def__init__(self):self.x=0self.y=0Point.__setattr__=self._setattrdef_setattr(self,name,value):ifnothasattr(self,name):raiseAttributeError("'"+name+"'notanattributeofPointobject.")else:super(Point,self).__setattr__(name,value)有没有

python - 从同一目录导入 __init__.py 和模块时 python 2.7 和 3.3+ 之间的区别

最近我遇到一个问题signalIwasusingfromflask-securitywasnotbehavingasexpected在python3.3。在查看flask-security的源代码时,我注意到我从flask-security包中的模块导入的信号也被导入到__init__.py中。通过从包的顶层导入信号,我能够解决我的问题(因为信号是在包初始化时导入的)。如果我运行以下代码:fromflask.ext.securityimportuser_registeredfromflask.ext.security.signalsimportuser_registeredasuser

python - 使用 __new__ 覆盖子类中的 __init__

我对使用__new__功能将代码注入(inject)子类的__init__函数很感兴趣。我从文档中了解到,python将在__new__返回的实例上调用__init__。但是,我在从__new__返回实例之前更改实例中__init__的值的努力似乎不起作用。classParent(object):def__new__(cls,*args,**kwargs):new_object=super(Parent,cls).__new__(cls)user_init=new_object.__init__def__init__(self,*args,**kwargs):print("New__i

python - 导入tensorflow报错: terminate called after throwing an instance of 'Xbyak::Error'

我正在尝试使用tensorflow调试错误。当我导入tensorflow时我收到以下错误importtensorflowastfterminatecalledafterthrowinganinstanceof'Xbyak::Error'what():internalerrorAborted(coredumped)这是安装细节操作系统>>Ubuntu14.04安装方法Anaconda>>conda4.4.11(condainstalltensorflow)python3--version>>Python3.6.4::Anaconda,Inc.如果有人有解决此问题的经验或知识,我将不胜感激

python - "ValueError: Trying to share variable $var, but specified dtype float32 and found dtype float64_ref"尝试使用 get_variable 时

我正在尝试构建自定义变分自动编码器网络,其中我使用来自编码器层的权重转置来初始化解码器权重,我找不到tf.contrib.layers的原生内容.fully_connected所以我使用了tf.assign,这是我的层代码:definference_network(inputs,hidden_units,n_outputs):"""Layerdefinitionfortheencoderlayer."""net=inputswithtf.variable_scope('inference_network',reuse=tf.AUTO_REUSE):forlayer_idx,hidden

python - 在包的 __init__.py 中导入子模块时出现奇怪的命名空间污染

主要.py:importpackage包/__init__.py:#usefunctiontosplitlocalandglobalnamespacedefdo_import():printglobals().keys()printlocals().keys()importfooasmodprintlocals().keys()printglobals().keys()do_import()包/foo.py:print'Hellofromfoo'执行main.py会输出如下:['__builtins__','__file__','__package__','__path__','__n

Python 多处理 - AssertionError : can only join a child process

我第一次涉足pythonmutliprocessing模块,但遇到了一些问题。我非常熟悉线程模块,但我需要确保我正在执行的进程是并行运行的。这是我正在尝试做的事情的概要。请忽略未声明的变量/函数之类的东西,因为我无法完整粘贴我的代码。importmultiprocessingimporttimedefwrap_func_to_run(host,args,output):output.append(do_something(host,args))returndeffunc_to_run(host,args):returndo_something(host,args)defdo_work(

python - 为什么 Fraction 使用 __new__ 而不是 __init__?

我正在尝试创建一个新的不可变类型,类似于内置的Fraction但不是派生自它。分数类iscreatedlikethis:#We'reimmutable,souse__new__not__init__def__new__(cls,numerator=0,denominator=None):...self=super(Fraction,cls).__new__(cls)self._numerator=...self._denominator=...returnself但是我看不出这和有什么不同def__init__(self,numerator=0,denominator=None):..

Python Selenium ConnectionResetError : [WinError 10054] An existing connection was forcibly closed by the remote host

我正在使用python3.6并使用最新版本的chromedriver,我尝试使用旧版本的chromedriver,我遇到了同样的问题,重新启动了我的电脑,同样的问题。这是我运行以重现错误的代码:fromseleniumimportwebdriverdriver=webdriver.Chrome()driver.get("https://google.com")完整错误:driver.get("https://google.com")File"C:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py",lin

python - multiprocessing.Process(使用 spawn 方法): which objects are inherited?

文档(python3.4)解释说,使用spawn,“子进程将仅继承运行进程对象的run()方法所需的那些资源”。但是哪些对象是“必要的”?我阅读它的方式向我表明,可以从run()内部访问的所有对象都是“必需的”,包括作为args传递给Process的参数.__init__,以及存储在全局变量中的任何内容,以及在全局范围内定义的类、函数及其属性。但是,这是不正确的;以下代码确认存储在全局变量中的对象没有被继承:#runningunderpython3.4/Windows#butbehavesthesameunderUniximportmultiprocessingasmpx=0class