I wrote some Python code which works but Pylint doesn't like the star. It keeps telling me:
Used * or ** magic (star-args)
Is it possible to write my code without the star? Some info: I'm using lxml; self.xml is an objectified XML file.
@property
def version_string(self):
'''Return the version as a string.'''
try:
version_format = self.xml.version.get("format")
except AttributeError:
return None
version_values = (v.text for v in self.xml.version.v)
return version_format.format(*version_values)
As expected, the floating point number (1.9876) was rounded up to two decimal places – 1.99. So %. 2f means to round up to two decimal places. You can play around with the code to see what happens as you change the number in the formatter.
The %d operator is used as a placeholder to specify integer values, decimals, or numbers. It allows us to print numbers within strings or other values. The %d operator is put where the integer is to be specified. Floating-point numbers are converted automatically to decimal values.
Python uses C-style string formatting to create new, formatted strings. The "%" operator is used to format a set of variables enclosed in a "tuple" (a fixed size list), together with a format string, which contains normal text together with "argument specifiers", special symbols like "%s" and "%d".
Python f-strings is introduced to have minimal syntax for string formatting. The expressions are evaluated at runtime. If you are using Python 3.6 or higher version, you should use f-strings for all your string formatting requirements.
There's nothing wrong with the splat operator. Without knowing what the version_format
function does, it's not possible to say if you could pass an iterable, or iterate the function directly, but frankly there's no reason to.
If you don't like that pylint warning, disable it. It was originally introduced because having lots of
def some_function(*args, **kwargs):
pass
lowers the readability / maintainability of the code.
star-args (W0142) is no longer present in pylint (at least since version 1.4.3). It appears to have been removed fairly recently.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With