草庐IT

example-new

全部标签

python - django-social-auth : How to redirect example. com 到 127.0.0.1 :8000?

我相信许多Django开发人员在使用社交身份验证时一定会遇到这个问题。最初当你开发它时,你想在你的本地服务器上测试它,因此你会在你的etc/hosts中重定向域名。我在文档中发现了这一点:https://github.com/omab/django-social-auth#facebookIfyoudefinearedirectURLinFacebooksetuppage,besuretonotdefinehttp://localhost:8000becauseitwon'tworkwhentesting.InsteadIdefinehttp://myapp.comandsetupam

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 - 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

python - 在从元类调用的类方法中调用 `super`.__new__

我有一个案例,我的类有一个自定义元类,它在创建它时调用类的类方法,比如:classMetaclass(type):def__new__(cls,name,bases,attrs):...new_class=super(Metaclass,cls).__new__(cls,name,bases,attrs)...new_class.get_fields()#dosomething...returnnew_classclassFooBar(object):__metaclass__=Metaclass@classmethoddefget_fields(cls):...(此类代码的示例在Ta

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):..