Other uses of the __name__ attribute

I learn Python by reading Dive Into Python on the Internet, and in chapter 2 I see that you can use the attribute __name__for testing purposes, so I was wondering if anyone knows of other uses for this attribute? Thanks

+3
source share
2 answers

I once had a decorator for a script command line file.

The decorator was used as follows:

@command
def someFunc():
    # code goes here

The decorator did something like this:

commands = {}
def command(func)
    commands[func.__name__] = func
    return func

And then I could do something like this:

commandName = raw_input()
commands[commandName]();

To read the command name from the console and call it - if earlier it was marked by the decorator (so dangerous calls would not be possible, as in the case with input).

( args, raw_input.)

, . - ", " . .

(BTW, there func.func_name), , func.__name__, , __, Python, AFAIK - )

+3

. ( ) , , , .

+1

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


All Articles