草庐IT

rb_str_new

全部标签

python - 使用元类的 __call__ 方法而不是 __new__?

在讨论元类时,thedocs状态:Youcanofcoursealsooverrideotherclassmethods(oraddnewmethods);forexampledefiningacustom__call__()methodinthemetaclassallowscustombehaviorwhentheclassiscalled,e.g.notalwayscreatinganewinstance.[编者注:这已从3.3的文档中删除。它在3.2中:Customizingclasscreation]我的问题是:假设我希望在调用类时具有自定义行为,例如缓存而不是创建新对象。我

python - 为什么 str.split 不接受关键字参数?

我遇到了这个——在我看来——奇怪的行为:"abc".split(maxsplit=1)TypeError:split()takesnokeywordarguments为什么str.split()不接受关键字参数,即使它是有意义的?我在Python2和Python3中都发现了这种行为。 最佳答案 见thisbug和itssuperseder.str.split()是CPython中的原生函数,因此表现出所描述的行为here:CPythonimplementationdetail:Animplementationmayprovidebu

python - 为什么 str.split 不接受关键字参数?

我遇到了这个——在我看来——奇怪的行为:"abc".split(maxsplit=1)TypeError:split()takesnokeywordarguments为什么str.split()不接受关键字参数,即使它是有意义的?我在Python2和Python3中都发现了这种行为。 最佳答案 见thisbug和itssuperseder.str.split()是CPython中的原生函数,因此表现出所描述的行为here:CPythonimplementationdetail:Animplementationmayprovidebu

python - 为什么 Python 新式类中的 __new__ 不是类方法?

Python2.2的变更日志(其中引入了新样式类)对__new__函数有以下说明:__new__isastaticmethod,notaclassmethod.Iinitiallythoughtitwouldhavetobeaclassmethod,andthat'swhyIaddedtheclassmethodprimitive.Unfortunately,withclassmethods,upcallsdon'tworkrightinthiscase,soIhadtomakeitastaticmethodwithanexplicitclassasitsfirstargument.但

python - 为什么 Python 新式类中的 __new__ 不是类方法?

Python2.2的变更日志(其中引入了新样式类)对__new__函数有以下说明:__new__isastaticmethod,notaclassmethod.Iinitiallythoughtitwouldhavetobeaclassmethod,andthat'swhyIaddedtheclassmethodprimitive.Unfortunately,withclassmethods,upcallsdon'tworkrightinthiscase,soIhadtomakeitastaticmethodwithanexplicitclassasitsfirstargument.但

python - 什么是 R 函数(如 str()、summary() 和 head())的 Python pandas 等价物?

我只知道describe()函数。有没有其他类似str()、summary()、head()的函数? 最佳答案 在pandas中,info()方法创建了一个与R的str()非常相似的输出:>str(train)'data.frame':891obs.of13variables:$PassengerId:int12345678910...$Survived:int0111000011...$Pclass:int3131331332...$Name:Factorw/891levels"Abbing,Mr.Anthony",..:1091

python - 什么是 R 函数(如 str()、summary() 和 head())的 Python pandas 等价物?

我只知道describe()函数。有没有其他类似str()、summary()、head()的函数? 最佳答案 在pandas中,info()方法创建了一个与R的str()非常相似的输出:>str(train)'data.frame':891obs.of13variables:$PassengerId:int12345678910...$Survived:int0111000011...$Pclass:int3131331332...$Name:Factorw/891levels"Abbing,Mr.Anthony",..:1091

python - 使用类的 __new__ 方法作为工厂 : __init__ gets called twice

我在python中遇到了一个奇怪的错误,将类的__new__方法用作工厂会导致实例化类的__init__方法被调用两次。这个想法最初是使用母类的__new__方法根据传递的参数返回她的一个child的特定实例,而不必在外部声明工厂函数类(class)。我知道使用工厂函数将是在这里使用的最佳设计模式,但在项目的这个阶段更改设计模式的成本会很高。因此,我的问题是:有没有办法避免对__init__的双重调用而在这种模式中只对__init__进行一次调用?classShape(object):def__new__(cls,desc):ifclsisShape:ifdesc=='big':ret

python - 使用类的 __new__ 方法作为工厂 : __init__ gets called twice

我在python中遇到了一个奇怪的错误,将类的__new__方法用作工厂会导致实例化类的__init__方法被调用两次。这个想法最初是使用母类的__new__方法根据传递的参数返回她的一个child的特定实例,而不必在外部声明工厂函数类(class)。我知道使用工厂函数将是在这里使用的最佳设计模式,但在项目的这个阶段更改设计模式的成本会很高。因此,我的问题是:有没有办法避免对__init__的双重调用而在这种模式中只对__init__进行一次调用?classShape(object):def__new__(cls,desc):ifclsisShape:ifdesc=='big':ret

python - 特殊方法的 Python 文档在哪里? (__init__, __new__, __len__, ...)

可以在类中使用的特殊双下划线/dunder方法的完整列表在哪里?(例如,__init__、__new__、__len__、__add__) 最佳答案 请查看specialmethodnamessection在Python语言引用中。 关于python-特殊方法的Python文档在哪里?(__init__,__new__,__len__,...),我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/ques