草庐IT

example-new

全部标签

python - 使用 pathlib 时,出现错误 : TypeError: invalid file: PosixPath ('example.txt' )

我正在使用Python3的pathlib模块,像这样:frompathlibimportPathfilename=Path(__file__).parent/"example.txt"contents=open(filename,"r").read()但我在某些机器上收到此错误:TypeError:invalidfile:PosixPath('example.txt')但在我的机器上它可以工作。 最佳答案 pathlib仅在Python3.6及更高版本中与open无缝集成。来自Python3.6'sreleasenotes:Theb

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

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

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

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

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 - 使用类的 __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

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

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

python - 定义元类时是否有任何理由选择 __new__ 而不是 __init__ ?

我总是像这样设置元类:classSomeMetaClass(type):def__new__(cls,name,bases,dict):#dostuffhere但我刚刚遇到了一个这样定义的元类:classSomeMetaClass(type):def__init__(self,name,bases,dict):#dostuffhere有什么理由比另一个更喜欢一个吗?更新:请记住,我问的是在元类中使用__new__和__init__。我已经了解了他们在另一个类(class)中的区别。但是在元类中,我不能使用__new__来实现缓存,因为__new__仅在元类中创建类时调用。