Python Forum
[solved] unexpected character after line continuation character
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[solved] unexpected character after line continuation character
#1
Hi

To higlight reading by adding comments, I'm looking for a way to split function arguments into several lines; nevertheless using backslash I'm facing to an error (I'm still digging on internet in how to fix it): is there a workaround?

Thanks

Paul

mymodule.myfunction(path, file, arg1, arg2)

mymodule.myfunction(path,   \# comment1
                    file,     \# comment2
                    arg1,   \# comment3
                    arg2)
Error:
SyntaxError: unexpected character after line continuation character
Reply
#2
remove the comments
Reply
#3
Hi @paul18fr ,
The backslash ( \ ) takes away the special meaning of the following character. It "escapes" the following character. (Or in some cases, it adds a special meaning to the following character (like "\r") but that is not the case here.)
Usually you can continue a line by putting a backslash before the newline. This takes away the special meaning of the newline: "execute the line".
But this is not needed here because a parenthesis is opened and Python understands it must continue until the closing parenthesis is seen.

So in this case you can just omit the backslashes. (In your example you placed the backslash before the hash sign (#), taking away the special meaning of the hash: "start comment".)
Reply
#4
Thanks ibreeden for your help; at least a relevant advise
Reply
#5
I agree with the "remove the comments" suggestion. If you need a comment to describe each argument I see this as a naming problem more than a comment problem. You should have a nice docstring describing the purpose of a module. A nice docstring describing how to use a function. For a class you should document each visible attribute. After that, I don't think good code should have many comments. Code should be obvious. If you find yourself having to comment the code to make the meaning clear, take some time to think about ways to improve the code. Well documented confusing code is still confusing code.

Using named arguments goes a long way towards clearing any confusion about function arguments. This is a function I wrote for a Connect 4 game.
def drop(column:int, player:int)->tuple[int, int]:
    '''Drop player's marker in column.  Marker falls to first open row.
    return resting spot.

    Arguments:
        column : Column chosen to play the marker. 0..COLUMNS-1
        player : Player 0 or 1

    Returns
        row and column where marker is located or None if
        column is full.
    '''
    for row in range(ROWS):
        if BOARD[column][row] is EMPTY:
            BOARD[column][row] = player
            return(column, row)
    return None
From an interactive environment I can ask to see the documentation for this function.
Output:
>>> help(drop) Help on function drop in module __main__: drop(column: int, player: int) -> tuple Drop player's marker in column. Marker falls to first open row. return resting spot. Arguments: column : Column chosen to play the marker. 0..COLUMNS-1 player : Player 0 or 1 Returns row and column where marker is located or None if column is full.
The function is called like this in the code:
    while True:
        try:
            col = int(input(f'{MARKERS[player]}: '))-1
        except ValueError:
            print(f'Enter integer [1:{COLUMNS}]')
        else:
            if spot := drop(column=col, player=player):
                return spot
            print(f'{col} is full')
There is no need to comment the arguments because they are obvious. col is the column argument and player is the player argument. If I want to find out what these arguments do I should look at the documentation for the drop() function, not some possibly incorrect comments where the function is used.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python rule about the space character surrounding the equal sign ineuw 10 1,515 Sep-21-2023, 09:17 AM
Last Post: ineuw
  How do I handle escape character in parameter arguments in Python? JKR 6 1,039 Sep-12-2023, 03:00 AM
Last Post: Apoed2023
Question UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 562: ord ctrldan 23 4,605 Apr-24-2023, 03:40 PM
Last Post: ctrldan
  use of escape character in re.sub and find WJSwan 1 876 Feb-16-2023, 05:19 PM
Last Post: Larz60+
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,305 Sep-27-2022, 01:38 PM
Last Post: buran
  UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 34: character Melcu54 7 18,305 Sep-26-2022, 10:09 AM
Last Post: Melcu54
  Unsupported Format Character Led_Zeppelin 2 5,183 Sep-20-2022, 01:46 PM
Last Post: deanhystad
  how to read chinese character? kucingkembar 2 1,312 Aug-25-2022, 08:42 PM
Last Post: kucingkembar
  How to split the input taken from user into a single character? mHosseinDS86 3 1,137 Aug-17-2022, 12:43 PM
Last Post: Pedroski55
  UnicodeEncodeError: 'ascii' codec can't encode character '\xfd' in position 14: ordin Armandito 6 2,643 Apr-29-2022, 12:36 PM
Last Post: Armandito

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020