草庐IT

new_size

全部标签

python - 为什么 object.__new__ 在这三种情况下的工作方式不同

来自问题Whydoesorratherhowdoesobject.__new__workdifferentlyinthesetwocases作者感兴趣的不是为什么,而是如何。我非常想知道为什么,特别是:为什么object.__init__没有打印参数而不是object.__new__(在testclass1中)为什么没有为testclass3引发错误?(因为它除了self之外不接受任何参数)代码>>>classtestclass1(object):...pass...>>>classtestclass2(object):...def__init__(self,param):...pas

python - 创建子图时 Matplotlib "dictionary changed size during iteration"错误

我写了一个函数来绘制由两个不同大小的子图组成的图形:defdraw_plot(data,function,sigma_value):gs=gridspec.GridSpec(1,5)ax1=subplot(gs[0,0:3])ax2=subplot(gs[0,3:5],sharey=ax1)gs.update(wspace=0.05)...我应该提到这是一个模块级函数,所以在该模块的顶部我进行了导入frompylabimport*importmatplotlib.gridspecasgridspec当我运行myplot.draw_plot(...),我得到RuntimeError.问题

python - 从 __new__ 返回 None 可以吗?

一般来说,如果类的用户知道有时构造函数的计算结果为None,那么从__new__方法返回None是否合理?文档并不暗示它是非法的,而且我没有看到任何直接的问题(因为__init__不会被调用,None不是有问题的自定义类的实例!).但是我担心是否有其他不可预见的问题让构造函数返回None是否是一个好的编程习惯具体例子:classMyNumber(int):def__new__(cls,value):#valueisastring(usually)parsedfromafileifvalue=='N.A.':returnNonereturnint.__new__(cls,value)

imp.new_module() 的 Python importlib 模拟

PyCharm告诉我imp已被弃用,所以我想知道是否有任何importlib的imp.new_module类似物。 最佳答案 引自documentation(Emphasismine)-imp.new_module(name)Returnanewemptymoduleobjectcalledname.Thisobjectisnotinsertedinsys.modules.Deprecatedsinceversion3.4:Usetypes.ModuleTypeinstead.例子->>>importtypes>>>types.Mo

python - 'index 0 is out of bounds for axis 0 with size 0' 是什么意思?

我是python和numpy的新手。我运行了我编写的代码,我收到了这条消息:'索引0超出了大小为0的轴0的范围'没有上下文,我只想弄清楚这是什么意思。问这个问题可能很愚蠢,但是轴0和大小0是什么意思?索引0表示数组中的第一个值..但我无法弄清楚轴0和大小0是什么意思。“数据”是一个文本文件,在两列中包含大量数字。x=np.linspace(1735.0,1775.0,100)column1=(data[0,0:-1]+data[0,1:])/2.0column2=data[1,1:]x_column1=np.zeros(x.size+2)x_column1[1:-1]=xx_colum

python - Python 的 __new__ 有哪些用例?

我了解__new__的作用(以及它与__init__的不同之处)所以我对定义不感兴趣,我感兴趣的是何时以及如何使用__new__。文档说:Ingeneral,youshouldn'tneedtooverride__new__unlessyou'resubclassinganimmutabletypelikestr,int,unicodeortuple但我想不出在其他情况下使用__new__或如何正确使用它(例如,子类化不可变类型时或为什么在这种情况下需要它)。那么,什么时候、为什么以及如何需要使用__new__?我感兴趣的是用例,而不是它的作用(我知道它的作用)。

python - Django UpdateView/ImageField 问题 : not returning new uploaded image

型号:classLogo(models.Model):media=models.ImageField(upload_to='uploads')def__unicode__(self):returnself.media.url查看:classLogoEdit(UpdateView):model=Logotemplate_name='polls/logo-edit.html'success_url='/polls/logos/'defform_valid(self,form):pdb.set_trace()模板:{%csrf_token%}{{form.as_p}}选择新图像:form调试

python - 是否可以覆盖枚举中的 __new__ 以将字符串解析为实例?

我想将字符串解析为python枚举。通常人们会实现一个解析方法来这样做。几天前,我发现了能够根据给定参数返回不同实例的__new__方法。这是我的代码,它不会工作:importenumclassTypes(enum.Enum):Unknown=0Source=1NetList=2def__new__(cls,value):if(value=="src"):returnTypes.Source#elif(value=="nl"):returnTypes.NetList#else:raiseException()def__str__(self):if(self==Types.Unknown

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 - 什么会导致 asyncio.new_event_loop() 的简单调用挂起?

我正在使用以下函数来强制协程同步运行:importasyncioimportinspectimporttypesfromasyncioimportBaseEventLoopfromconcurrentimportfuturesdefawait_sync(coro:types.CoroutineType,timeout_s:int=None):""":paramcoro:acoroutineorlambdaloop:coroutine(loop):paramtimeout_s::return:"""loop=asyncio.new_event_loop()#type:BaseEventL