博文

目前显示的是标签为“python”的博文

What is the use of code if __name__ == '__main__'?

What is the use of code if __name__ == '__main__'? When the Python interpreter reads a source file, it executes all of the code found in it. Before executing the code, it will define a few special variables. For example, if the python interpreter is running that module (the source file) as the main program, it sets the special __name__ variable to have a value "__main__". If this file is being imported from another module, __name__ will be set to that module's name. In the case of your script, let's assume that it's executing as the main function, e.g. you execute something like python threading_example.py on the command line. After setting up the special variables, it will execute the import statement and load those modules. It will then evaluate the def block, creating a function object and creating a variable called myfunction that points to the function object. It will then read the if statement and see that __name__ does equal "__main__...

What is __init__.py used for?

Files named __init__.py are used to mark directories on disk as Python package directories. For example, suppose you have these files: dir/spam/__init__.py dir/spam/module.py and dir is on your python path, then you can import the module.py as import spam.module or from spam import module If you remove the __init__.py file, Python will no longer look for submodules inside that directory, so attempts to import the module will fail. The __init__.py file is usually empty, but can be used to export selected portions of the package under more convenient name, hold convenience functions, etc. Given the example above, the contents of the init module can be accessed as import spam Reference http://effbot.org/pyfaq/what-is-init-py-used-for.htm

Python 正则表达式反斜杠的麻烦

在早期规定中,正则表达式用反斜杠字符 ("\") 来表示特殊格式或允许使用特殊字符而不调用它的特殊用法。这就与 Python 在字符串中的那些起相同作用的相同字符产生了冲突。 让我们举例说明,你想写一个 RE 以匹配字符串 "\section",可能是在一个 LATEX 文件查找。为了要在程序代码中判断,首先要写出想要匹配的字符串。接下来你需要在所有反斜杠和其它元字符前加反斜杠来取消其特殊意义,结果要匹配的字符串 就成了"\\section"。 当把这个字符串传递给re.compile()时必须还是"\\section"。然而,作为Python的字符串实值(string literals)来表示的话,"\\section"中两个反斜杠还要再次取消特殊意义,最后结果就变成了"\\\\section"。 字符 阶段 \section 要匹配的字符串 \\section 为 re.compile 取消反斜杠的特殊意义 "\\\\section" 为"\\section"的字符串实值(string literals)取消反斜杠的特殊意义 简单地说,为了匹配一个反斜杠,不得不在 RE 字符串中写 '\\\\',因为正则表达式中必须是 "\\",而每个反斜杠在常规的 Python 字符串实值中必须表示成 "\\"。在 REs 中反斜杠的这个重复特性会导致大量重复的反斜杠,而且所生成的字符串也很难懂。 解决的办法就是为正则表达式使用 Python 的 raw 字符串表示;在字符串前加个 "r" 反斜杠就不会被任何特殊方式处理,所以 r"\n" 就是包含"\" 和 "n" 的两个字符,而 "\n" 则是一个字符,表示一个换行。正则表达式通常在 Python 代码中都是用这种 raw 字符串表示。 常规字符串 Raw 字符串 "ab*" r"ab*" "\\\\section...

Accessing Jython from Java Without Using jythonc

You may or may not know that it is possible to access Jython code from Java without compiling it using the jythonc utility. This technique is possible using a mixture of Java interfaces and usage of the  PythonInterpreter . As a matter of fact, I believe that using this technique correctly is more effective than using jythonc. To put it simply, to use this technique you must create a "factory" method which uses the  PythonInterpreter  class to interpret a .py module for use within Java code. Any Java code that uses the Jython code should be coded against an interface which is implemented by the Jython code. In order to provide a fully functional example, I've created a simple application with hard-coded data. This application shows the potential for using this technique within your Java applications to have the ability for use of dynamic Jython objects. The application is simply called "jyinterface" and it contains four pieces of code: -Main.java - Jyt...