Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible To Format A List Without * Magic?

Tags:

python

pylint

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)
like image 777
Mr.Yeah Avatar asked May 18 '14 19:05

Mr.Yeah


People also ask

What is .2f in Python?

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.

How do you use %d in Python?

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.

What are format specifiers in Python?

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

Should I use F string Python?

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.


3 Answers

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.

like image 61
Marcin Avatar answered Nov 16 '22 05:11

Marcin


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.

like image 22
gurney alex Avatar answered Nov 16 '22 06:11

gurney alex


star-args (W0142) is no longer present in pylint (at least since version 1.4.3). It appears to have been removed fairly recently.

like image 4
Six Avatar answered Nov 16 '22 06:11

Six