What is “import this, what, other, things”?

#!/usr/bin/env python import this, that, other, stuff class SomeObject(object): pass def some_function(*args,**kwargs): pass if __name__ == '__main__': print( "This only executes when %s is executed rather than imported" % __file__) 

What does the above code do? I get the output as follows

 The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let do more of those! 

I am new to python, but very curious to find out. Please help me.

+5
source share
4 answers

Try the following:

 import this 

Only this line will make the Zen of Python interpreter output

+5
source

if in the python interpreter you do the following: 1) help () 2) modules - you see a list of modules, "this" - one in the list; just enter "this" (without quotes) - it contains the ZEN of Python, which you get here on your output; lone import this will give you the same result. But this is a place where you can verify that in python modules, enter any given module name and see. The code is just an example of a bear.

0
source
 import this 

gives you Zen of Python from Tim Peters. Since this is an import, regardless of how your file is executed (directly or by import), your if __name__ == '__main__': block is incorrect in its print() call. Your code will also throw an error if your computer does not have that.py , other.py and stuff.py , but regardless of whether you execute it directly or import it, the result will be the same. These modules ( that , other and stuff ) are not Easter eggs :)

0
source

Despite the poem, it is a print function encoded in this.py as follows:

 $ touch test.py $ echo "print('Zen of Python')" > test.py $ python -c "import test" Zen of Python 
0
source

Source: https://habr.com/ru/post/1246938/


All Articles