Asked  7 Months ago    Answers:  5   Viewed   20 times

What is the correct name for operator *, as in function(*args)? unpack, unzip, something else?



In Ruby and Perl 6 this has been called "splat", and I think most people from those communities will figure out what you mean if you call it that.

The Python tutorial uses the phrase "unpacking argument lists", which is long and descriptive.

It is also referred to as iterable unpacking, or in the case of **, dictionary unpacking.

Tuesday, June 1, 2021
answered 7 Months ago

No, you can't create new operators. However, if you are just evaluating expressions, you could process the string yourself and calculate the results of the new operators.

Saturday, June 5, 2021
answered 6 Months ago

You probably want to line up with the """

def foo():
    string = """line one
             line two
             line three"""

Since the newlines and spaces are included in the string itself, you will have to postprocess it. If you don't want to do that and you have a whole lot of text, you might want to store it separately in a text file. If a text file does not work well for your application and you don't want to postprocess, I'd probably go with

def foo():
    string = ("this is an "
              "implicitly joined "

If you want to postprocess a multiline string to trim out the parts you don't need, you should consider the textwrap module or the technique for postprocessing docstrings presented in PEP 257:

def trim(docstring):
    if not docstring:
        return ''
    # Convert tabs to spaces (following the normal Python rules)
    # and split into a list of lines:
    lines = docstring.expandtabs().splitlines()
    # Determine minimum indentation (first line doesn't count):
    indent = sys.maxint
    for line in lines[1:]:
        stripped = line.lstrip()
        if stripped:
            indent = min(indent, len(line) - len(stripped))
    # Remove indentation (first line is special):
    trimmed = [lines[0].strip()]
    if indent < sys.maxint:
        for line in lines[1:]:
    # Strip off trailing and leading blank lines:
    while trimmed and not trimmed[-1]:
    while trimmed and not trimmed[0]:
    # Return a single string:
    return 'n'.join(trimmed)
Wednesday, June 9, 2021
answered 6 Months ago
#!/usr/bin/env python

is more portable because in general the program /usr/bin/env can be used to "activate" the desired command without full path.

Otherwise, you would have to specify the full path of the Python interpreter, which can vary.

So no matter if the Python interpreter was in /usr/bin/python or in /usr/local/bin/python or in your home directory, using #!/usr/bin/env python will work.

Tuesday, July 27, 2021
answered 5 Months ago

In Python 2.x, the default division operator is "Classic division". This means that /, when used with integer operators will result in integer division similar to C++ or java [i.e. 4/3 = 1].

In Python 3.x, this is changed. There, / refers to "True division" [4/3 = 1.3333..], whereas // is used to request "Classic/Floor division".

If you want enable "True division" in Python 2.7, you can use from __future__ import division in your code.

Source: PEP 238

For example:

>>> 4/3
>>> 4//3
>>> from __future__ import division
>>> 4/3
>>> 4//3
Tuesday, August 24, 2021
Jeffrey Stilwell
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :