[Python-ideas] start, test, init
Ron Adam
ron3200 at gmail.com
Mon Dec 2 16:37:37 CET 2013
On 12/02/2013 06:35 AM, spir wrote:
>>
>> As for the testing case... I'd like for python to have a -t option that only
>> sets a global name __test__ to True. Then that covers most of your use
>> cases
>> without adding a whole lot. Sometimes less is more.
>>
>> if __test__:
>> test()
>> elif __name__ == '__main__':
>> main()
>>
>> That's just one possible combination of using __test__ and __name__.
>
> That is all right, in my view; except maybe the point on flexibility (the
> solution of std func names is highly flexible). However, this does not
> address 2 related issues:
> * Why has the idiom "(el)if __name__ == '__main__':" has to be that
> obscure? and involve unnecessary knwoledge of Python arcanes (for a newcomer)
I don't see it as obscure or arcane to compare a string to variable which
bound to a string. It's a very common and basic operation.
The only extra knowledge you need is to know that the __name__ attribute is
set to "__main__" in the main module. And that enables us to check if
"this" is the main module in a direct way.
>>> dir()
['__builtins__', '__doc__', '__loader__', '__name__', '__package__']
>>> __name__
'__main__'
>>> __name__ == '__main__'
True
The difference between python and compiled languages that must use
functions to get things started, is that python is already executing the
code, so it a matter of avoiding some parts at different times.
> * Having a std (albeit not required) high-level software structure is a net
> gain for the community of Python programmers (instead of everyone doing
> their own way... when they do).
Most beginner books on python explain objects and name binding fairly
early. Knowing a module is also an object and has a __name__ attribute is
something a python programmer should learn very early. It helps them learn
and understand how everything works in a more direct way. So rather than
adding ... "You been doing this for a while, but many people do this other
way ... ". So it would need to be explained anyway.
Cheers,
Ron
More information about the Python-ideas
mailing list