Search code examples
pythoncoding-stylepylint

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


Solution

  • ** 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.