7

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

4

3 に答える 3

4

**それは何でも受け入れるので、いくつかのよりトリッキーなバグを導入することができます。通常、間違って呼び出されたときに壊れたコードが必要です。次に例を示します。

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)

**引数名を入力するのが面倒だからといって使用しないでください。ただし、それが常に可能であるとは限らないため、正当な用途もあります。

于 2011-07-21T08:52:09.220 に答える
2

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.

于 2011-07-21T08:44:01.643 に答える
2

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

于 2011-07-21T08:46:28.853 に答える