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 ba开发者_如何学Cd styling for writing python code? Is there some kind of standard replacement for using this ?
Regards, Bogdan
**
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.
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.
**
excellent for what it is designed for: to forward arguments to other functions. You can definitely do bad things that will decrease the readability of your code with it, but it's not considered bad practice per se.
精彩评论