草庐IT

internal_init

全部标签

python - 如何只导入包中没有 exec __init__.py 的子模块

当从包中导入子模块时,包文件夹中的__init__.py文件将首先执行,我该如何禁用它。有时我只需要一个包中的一个功能,导入整个包有点重。例如,pandas.io.clipboard模块不依赖于pandas中的任何其他函数。frompandas.io.clipboardimportclipboard_get将导入函数,但也会导入所有pandas公共(public)模块。是否有一些方法可以只导入剪贴板模块,因为它是我自己的应用程序文件夹中的一个模块。 最佳答案 没有,设计。如果你想在导入子模块时避免太多开销,你只需使用空的__init

python - 如何模拟父类(super class)的 __init__ 创建一个包含用于单元测试的模拟对象的属性?

我正在尝试为类的__init__编写单元测试:def__init__(self,buildNum,configFile="configfile.txt"):super(DevBuild,self).__init__(buildNum,configFile)ifconfigFile=="configfile.txt":self.config.MakeDevBuild()config属性由super的__init__设置。我正在使用mock,并且我希望config属性是一个模拟对象。但是,我一直无法弄清楚如何真正实现这一目标。这是我能想到的最好的测试:deftest_init(self):

python 类变量在 __init__ 中不可见?

此代码会产生一条错误消息,令我感到惊讶:classFoo(object):custom=1def__init__(self,custom=Foo.custom):self._custom=customx=Foo()谁能开导一下? 最佳答案 Foo是不可见的,因为您正在构建它。但是由于您与custom处于同一范围内,因此您可以只说custom而不是Foo.custom:classFoo(object):custom=1def__init__(self,mycustom=custom):self._custom=mycustom但请注意

python - 凯拉斯 |类型错误 : __init__() missing 1 required positional argument: 'nb_col'

我目前正在尝试将本教程代码实现到我自己的convnet.py中,但出现错误。Tutorial这是完整的错误:Traceback(mostrecentcalllast):File"convnet.py",line6,inmodel.add(Conv2D(32,(3,3),input_shape=(3,150,150)))TypeError:__init__()missing1requiredpositionalargument:'nb_col'这是程序出错的前10行:fromkeras.modelsimportSequentialfromkeras.layersimportConv2D,

python - 在 Python 中子类化 float 类型,无法在 __init__() 中捕获异常

在Python2.5上,我需要通过修改后的__str__()方法来使用float。我还需要知道构造函数何时失败。为什么我无法捕获float.__init__()引发的异常?查询派生浮点对象数值的最佳方法是什么?在我的代码中,我使用了float(self)。classMy_Number(float):def__init__(self,float_string):try:super(My_Number,self).__init__(float_string)except(TypeError,ValueError):raiseMy_Error(float_string)def__str__(

python - SQLAlchemy __init__ 未运行

我有以下代码:session=scoped_session(sessionmaker(autocommit=False,autoflush=True,bind=engine))Base=declarative_base()Base.query=session.query_property()classCommonBase(object):created_at=Column(DateTime,default=datetime.datetime.now)updated_at=Column(DateTime,default=datetime.datetime.now,onupdate=dat

android - 错误 :rendering problems The following classes could not be found android. support.v7.internal.widget.ActionBarOverlayLayout

我只是android应用程序开发的初学者。当我在AndroidStudio1.1.0上创建一个新项目时,它放弃了这个错误"渲染问题找不到以下类android.support.v7.internal.widget.ActionBarOverlayLayout"现在我在谷歌上搜索了这个,我发现大多数人给出的可能是3个解决方案。他们说:要么将api(从预览窗口Pane)从22更改为21,要么将应用主题从“项目主题”更改为任何其他主题。确保在您的项目结构中导入了正确的appcompat-v7库->依赖项,引用这些步骤:将支持库功能项目标识符添加到依赖项部分。例如,要包含appcompat项目,

android - 错误 :rendering problems The following classes could not be found android. support.v7.internal.widget.ActionBarOverlayLayout

我只是android应用程序开发的初学者。当我在AndroidStudio1.1.0上创建一个新项目时,它放弃了这个错误"渲染问题找不到以下类android.support.v7.internal.widget.ActionBarOverlayLayout"现在我在谷歌上搜索了这个,我发现大多数人给出的可能是3个解决方案。他们说:要么将api(从预览窗口Pane)从22更改为21,要么将应用主题从“项目主题”更改为任何其他主题。确保在您的项目结构中导入了正确的appcompat-v7库->依赖项,引用这些步骤:将支持库功能项目标识符添加到依赖项部分。例如,要包含appcompat项目,

python - 从 __init__() 调用重写方法是否安全?

这是thisquestion的跟进:EffectiveJava2ndEdition,第17条:继承的设计和文档,否则禁止:Thereareafewmorerestrictionsthataclassmustobeytoallowinheritance.Constructorsmustnotinvokeoverridablemethods,directlyorindirectly.Ifyouviolatethisrule,programfailurewillresult.Thesuperclassconstructorrunsbeforethesubclassconstructor,so

Python:为 __init__ 扩展 int 和 MRO

在Python中,我试图扩展内置的“int”类型。在这样做时,我想将一些关键字参数传递给构造函数,所以我这样做:classC(int):def__init__(self,val,**kwargs):super(C,self).__init__(val)#Dosomethingwithkwargshere...然而,虽然调用C(3)工作正常,但C(3,a=4)给出:'a'isaninvalidkeywordargumentforthisfunction`和C.__mro__返回预期的:(,,)但似乎Python试图先调用int.__init__...有人知道为什么吗?这是解释器中的错误吗