Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python styling ** question

I'm working on my first project using Python 2.7. We're coming from a Java background and our first instinct was to write python code in a Java-esque way. But now we're trying to adapt as much as possible. So far we are using pylint to adapt our code.

Now I keep running into a situation with pylint. Every time I use something like **data to pass values to a method I'm getting a pylint warning about using * or **. Now my question is: Is using ** a bad styling for writing python code? Is there some kind of standard replacement for using this ?

Regards, Bogdan

like image 443
Bogdan Avatar asked Jul 21 '11 08:07

Bogdan


2 Answers

** can introduce some more tricky bug because it will accept anything. Usually you want code that breaks when called wrong. Here is a example:

def dostuff(**kwargs):
 force = 3
 if kwargs.get('reallyhard', False):
     force += 5 
 # and so on

# Now you need luck to find this bug  ...
dostuff(fancy=True, funky=False, realyhard=True)

You shouldn't use ** just because you are too lazy to type out the argument names. That is not always possible though, so there are legitimate uses too.

like image 193
Jochen Ritzel Avatar answered Oct 27 '22 10:10

Jochen Ritzel


It's almost impossible to use static analysis to verify that the arguments generated by ** are all valid, but if it is the only appropriate mechanism them by all means do use it.

like image 43
Ignacio Vazquez-Abrams Avatar answered Oct 27 '22 10:10

Ignacio Vazquez-Abrams