Asked  7 Months ago    Answers:  5   Viewed   20 times

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

 Answers

26

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
 
o_flyer
answered 7 Months ago
62

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
 
Daveel
answered 6 Months ago
72

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 "
              "string")

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:]:
            trimmed.append(line[indent:].rstrip())
    # Strip off trailing and leading blank lines:
    while trimmed and not trimmed[-1]:
        trimmed.pop()
    while trimmed and not trimmed[0]:
        trimmed.pop(0)
    # Return a single string:
    return 'n'.join(trimmed)
Wednesday, June 9, 2021
 
LukeP
answered 6 Months ago
17
#!/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
 
Gigamegs
answered 5 Months ago
90

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
1
>>> 4//3
1
>>> from __future__ import division
>>> 4/3
1.3333333333333333
>>> 4//3
1
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 :  
Share