草庐IT

new_folder

全部标签

python - thread.start_new_thread 与 threading.Thread.start

python中的thread.start_new_thread和threading.Thread.start有什么区别?我注意到,当调用start_new_thread时,新线程会在调用线程终止后立即终止。threading.Thread.start则相反:调用线程等待其他线程终止。 最佳答案 thread模块是Python的低级线程API。除非您确实需要,否则不建议直接使用它。threading模块是一个高级API,构建在thread之上。Thread.start方法实际上是使用thread.start_new_thread实现的

python - 在 Python 中覆盖 __new__ 和 __init__

这个问题在这里已经有了答案:关闭12年前。PossibleDuplicate:Python'suseof__new__and__init__?我的理解是,__init__与Java中的构造函数不同,因为__init__只初始化一个已经隐式构造的对象(因为__init__在__new__之后调用)。但是,我需要定义的所有内容都使用了Java中“构造函数”的后一个属性。在什么情况下程序员会想要覆盖__new__?编辑:为了记录,我问的部分原因是我想知道覆盖new与在已接受的中使用单独的类方法相比有什么优势/劣势回答这个问题:MovingBeyondFactoriesinPython

python - __new__ 生成类实例的不当使用?

我正在创建一些类来处理各种类型的文件共享(nfs、afp、s3、本地磁盘)等中的文件名。当用户输入时,我得到一个标识数据源的字符串(即"nfs://192.168.1.3"或"s3://mybucket/data")等我正在从具有公共(public)代码的基类中继承特定的文件系统。我感到困惑的地方在于对象的创建。我有以下内容:importosclassFileSystem(object):classNoAccess(Exception):passdef__new__(cls,path):ifclsisFileSystem:ifpath.upper().startswith('NFS:/

python - 为什么'new_file + = line + string'比'new_file = new_file + line + string'快得多?

这个问题已经有了答案:Whyisvariable1+=variable2muchfasterthanvariable1=variable1+variable2?1个答案当我们使用以下代码时,我们的代码需要10分钟来虹吸68000条记录:new_file=new_file+line+string但是,当我们执行以下操作时,只需1秒钟:new_file+=line+string代码如下:forlineincontent:importtimeimportcmdbrefname="STAGE050.csv"regions=cmdbre.regionsstart_time=time.time()

python - 无法使用 os.remove 删除文件夹(WindowsError : [Error 5] Access is denied: 'c:/temp/New Folder' )

我正在处理一个测试用例,我为其创建了一些子目录。但是,我似乎没有权限删除它们了。我的UA是管理员帐户(WindowsXP)。我第一次尝试:folder="c:/temp/"fordirinos.listdir(folder):os.remove(folder+dir)然后folder="c:/temp/"os.remove(folder+"NewFolder")因为我确定“新文件夹”是空的。但是,在所有情况下我都会得到:Traceback(mostrecentcalllast):File"",line3,inWindowsError:[Error5]Accessisdenied:'c:

python - 如何将 "New"Python 脚本选项添加到上下文菜单?

我正在尝试为Windows7上的新建->Python脚本添加上下文菜单选项。但是,我尝试的所有操作都失败了。我认为它应该工作的方式如下:添加以下注册表项:[HKEY_CLASSES_ROOT\.py\ShellNew]"FileName"="Template.py"空文件版本:[HKEY_CLASSES_ROOT\.py\ShellNew]"NullFile"=""可选注册表项[HKEY_CLASSES_ROOT\.py]"PerceivedType"="text/plain"@="PythonScript"将文件添加到Windows的ShellNew文件夹...虽然我以前做过,但它对

python - 从 Python C API 中的子类型向 tp_new 和 tp_init 传递参数

我最初在Pythoncapi-sig列表上问过这个问题:Howtopassargumentstotp_newandtp_initfromsubtypes?我正在阅读PythonPEP-253关于子类型化,还有很多关于如何构造类型、调用tp_new和tp_init槽等的好建议。但是,它缺少关于将参数从子类型传递到父类(superclass)型的重要说明。似乎PEP-253根据注释未完成:(XXXThereshouldbeaparagraphortwoaboutargumentpassinghere.)所以,我正在尝试推断一些策略wellknownfromthePythonclassess

python - __new__ 方法返回与其第一个参数类型不同的对象的用例是什么?

文档impliesthatit'sok__new__(cls,...)返回类型不同于cls的对象。它说在那种情况下__init__()不会被调用。它没有明确说明,但常识或简单测试证实生成的对象不会具有cls类型。为什么允许这种看似奇怪的行为?用例是什么?这显然是故意的。 最佳答案 当您为单元测试创​​建模拟对象时,它会很有用。您可以更改__new__方法以在某些情况下返回具有相同接口(interface)的另一个对象,与原始对象相同(例如模拟原始类实例的行为),而无需修改其余代码。 关于

python - 我如何正确地继承具有 __new__ 方法的父类(super class)?

假设我们有一个类'Parent',由于某种原因定义了__new__和一个继承自它的类'Child'。(在我的例子中,我试图从我无法修改的第3方类继承)classParent:def__new__(cls,arg):#...somethingimportantisdoneherewitharg我的尝试是:classChild(Parent):def__init__(self,myArg,argForSuperclass):Parent.__new__(argForSuperclass)self.field=myArg但是同时p=Parent("argForSuperclass")按预期工

python - Pandas 数据框 : how to apply describe() to each group and add to new columns?

df:namescoreA1A2A3A4A5B2B4B6B8想要以下面的形式获取以下新数据框:namecountmeanstdmin25%50%75%maxA53............B45............如何从df.describe()中提取信息并重新格式化?谢谢 最佳答案 还有更短的:)printdf.groupby('name').describe().unstack(1)Nothingbeatsone-liner:In[145]:printdf.groupby('name').describe().reset_in