What is

Python源代码目录中的__init__.py是什么? It's a part of a package. Here's the documentation. The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string , from unintentionally hiding valid modules that occur later (deeper) on the module search path. In the simplest case, __init_

什么是

Python源代码目录中的__init__.py是什么? 这是一个包的一部分。 这里是文档。 需要__init__.py文件才能使Python将目录视为包含包; 这是为了防止具有通用名称的目录(例如string )无意中隐藏模块搜索路径中稍后(更深)发生的有效模块。 在最简单的情况下, __init__.py可以只是一个空文件,但它也可以执行包的初始化代码或设置__all__变量,稍后介绍。 名为__init__.py文件用于将磁盘上的目录标记为Python包目录。 如

In Python what is a global statement?

What is a global statement ? And how is it used? I have read Python's official definition; however, it doesn't make a lot of sense to me. Every "variable" in python is limited to a certain scope. The scope of a python "file" is the module-scope. Consider the following: #file test.py myvariable = 5 # myvariable has module-level scope def func(): x = 3

在Python中,什么是全局声明?

什么是全球声明 ? 它是如何使用的? 我读过Python的官方定义; 然而,这对我来说并没有多大意义。 python中的每个“变量”都被限制在一定范围内。 python“文件”的范围是模块范围。 考虑以下: #file test.py myvariable = 5 # myvariable has module-level scope def func(): x = 3 # x has "local" or function level scope. 具有局部作用域的对象只要函数退出并且永远不会被检索(除非您return它们),但

purpose of 'if

This question already has an answer here: What does if __name__ == “__main__”: do? 23 answers Well, imagine that someone else wants to use the functions in your module in their own program. They import your module... and it starts doing its own thing! With the if __name__ == "__main__" , this doesn't happen. Your module only "does its thing" if it's run as the

目的'如果

这个问题在这里已经有了答案: 如果__name__ ==“__main__”:做什么? 23个答案 那么,想象别人想要在自己的程序中使用模块中的函数。 他们导入你的模块......并开始做自己的事情! 用if __name__ == "__main__" ,这不会发生。 如果它作为主模块运行,你的模块只会“做它的事情”。 否则,它的行为就像一个图书馆。 它通过简化代码来鼓励代码重用。 (正如@Sheng所提到的,您可能想要将模块导入到另一个脚

How to find if directory exists in Python

在Python的os模块中,有没有办法找到一个目录是否存在,如: >>> os.direxists(os.path.join(os.getcwd()), 'new_folder')) # in pseudocode True/False You're looking for os.path.isdir , or os.path.exists if you don't care whether it's a file or a directory. Example: import os print(os.path.isdir("/home/el")) print(os.path.exists("/home/el/myfile.txt")) So close! os.path.isdir re

如何查找Python中是否存在目录

在Python的os模块中,有没有办法找到一个目录是否存在,如: >>> os.direxists(os.path.join(os.getcwd()), 'new_folder')) # in pseudocode True/False 如果你不关心它是文件还是目录,你正在寻找os.path.isdir或者os.path.exists 。 例: import os print(os.path.isdir("/home/el")) print(os.path.exists("/home/el/myfile.txt")) 很近! 如果您传入当前存在的目录名称, os.path.isdir将返回True 。 如果它

Find current directory and file's directory

This question already has an answer here: How to properly determine current script directory? 11 answers How to know/change current directory in Python shell? 6 answers To get the full path to the directory a Python file is contained in, write this in that file: import os dir_path = os.path.dirname(os.path.realpath(__file__)) (Note that the incantation above won't work if you'v

查找当前目录和文件的目录

这个问题在这里已经有了答案: 如何正确确定当前脚本目录? 11个答案 如何知道/更改Python shell中的当前目录? 6个答案 要获取包含Python文件的目录的完整路径,请将该文件写入该文件中: import os dir_path = os.path.dirname(os.path.realpath(__file__)) (请注意,如果您已经使用os.chdir()更改当前工作目录,则上述咒语将不起作用,因为__file__常量的值相对于当前工作目录,并且不会被os.chdir()更改os.chdir

platform way of getting information from Python's OSError?

On a simple directory creation operation for example, I can make an OSError like this: (Ubuntu Linux) >>> import os >>> os.mkdir('foo') >>> os.mkdir('foo') Traceback (most recent call last): File "<stdin>", line 1, in <module> OSError: [Errno 17] File exists: 'foo' Now I can catch that error like this: >>> import os >>> os.mkdir('foo

从Python的OSError获取信息的平台方式?

例如,在一个简单的目录创建操作中,我可以像这样创建一个OSError: (Ubuntu Linux) >>> import os >>> os.mkdir('foo') >>> os.mkdir('foo') Traceback (most recent call last): File "<stdin>", line 1, in <module> OSError: [Errno 17] File exists: 'foo' 现在我可以捕捉到这样的错误: >>> import os >>> os.mkdir('foo') >>> try: ... o

Meaning of @classmethod and @staticmethod for beginner?

Could someone explain to me the meaning of @classmethod and @staticmethod in python? I need to know the difference and the meaning. As far as I understand, @classmethod tells a class that it's a method which should be inherited into subclasses, or... something. However, what's the point of that? Why not just define the class method without adding @classmethod or @staticmethod or any

@classmethod和@staticmethod对初学者的意义?

有人能向我解释@classmethod和@staticmethod在python中的含义吗? 我需要知道其中的差异和意义。 据我所知, @classmethod告诉一个类,它是一种应该被继承到子类或者...的方法。 但是,这有什么意义呢? 为什么不定义类方法而不添加@classmethod或@staticmethod或任何@定义? tl; dr:我应该什么时候使用它们,为什么要使用它们,我应该如何使用它们? 我用C ++很先进,所以使用更高级的编程概念应该不成问题。 如果可

What are the differences between type() and isinstance()?

What are the differences between these two code fragments? Using type() : import types if type(a) is types.DictType: do_something() if type(b) in types.StringTypes: do_something_else() Using isinstance() : if isinstance(a, dict): do_something() if isinstance(b, str) or isinstance(b, unicode): do_something_else() To summarize the contents of other (already good!) answers, isi

type()和isinstance()之间有什么区别?

这两个代码段有什么区别? 使用type() : import types if type(a) is types.DictType: do_something() if type(b) in types.StringTypes: do_something_else() 使用isinstance() : if isinstance(a, dict): do_something() if isinstance(b, str) or isinstance(b, unicode): do_something_else() 为了总结其他(已经很好!)答案的内容, isinstance迎合继承(派生类的一个实例也是基类的一个实例),

What is the difference between old style and new style classes in Python?

What is the difference between old style and new style classes in Python? Is there ever a reason to use old-style classes these days? From http://docs.python.org/2/reference/datamodel.html#new-style-and-classic-classes : Up to Python 2.1, old-style classes were the only flavour available to the user. The concept of (old-style) class is unrelated to the concept of type: if x is an instance o

Python中的旧风格和新风格类有什么区别?

Python中的旧风格和新风格类有什么区别? 这些日子里是否有过使用旧式课程的理由? 从http://docs.python.org/2/reference/datamodel.html#new-style-and-classic-classes: 在Python 2.1之前,老式的类是用户唯一可用的风格。 (old-style)类的概念与type的概念无关:如果x是旧式类的实例,则x.__class__指定x的类,但type(x)始终是<type 'instance'> 。 这反映了一个事实,即所有旧式实例独立于它们的类

What does the Star operator mean?

Possible Duplicate: What does *args and **kwargs mean? What does the * operator mean in Python, such as in code like zip(*x) or f(**k) ? How is it handled internally in the interpreter? Does it affect performance at all? Is it fast or slow? When is it useful and when is it not? Should it be used in a function declaration or in a call? The single star * unpacks the sequence/collecti

Star运算符是什么意思?

可能重复: * args和** kwargs是什么意思? *运算符在Python中的含义是什么,比如像zip(*x)或f(**k)这样的代码? 翻译人员在内部如何处理? 它会影响性能吗? 它是快还是慢? 它何时有用,何时不是? 是否应该在函数声明或通话中使用? 单星*将序列/集合解开成位置参数,所以你可以这样做: def sum(a, b): return a + b values = (1, 2) s = sum(*values) 这将解压元组,使其实际执行为: s = sum(1, 2)