Lugsole.net long logo

Unusual python syntax

Author: Lugsole

Lets first start with yes there are wierd things that are syntacticaly correct, but are ard to read. For example in c++ and javascript you can use inline functions and the && and || operator to performs if else statements. In python there are these list generators where it is possible to have a for loop inside of an array. Another wierd bit of syntax that is syntactially correct would be using the star opperator in a functions arguments. This might sound unusual or off, but it is syntactically correct. This might sound useless, but instead it is usefull in wierd edge casses. Lets say you are working on a GUI that has to call python functions without knowing there arguments before they run, or when they might have changed. You could use

1def fun(arg1, arg2, arg3=None, test="Hello", *args, **kwargs):
2    print("arg1",arg1)
3    print("arg2",arg2)
4    print("arg3",arg3)
5    print("test",test)
6
7fun(*args)