Question

Specifically can I provide append() a Null/None value in Python?

I am trying to add auto complete functionality to a command line application, so am using readline to obtain anything the user may have typed at the raw_input prompt.

I'm getting an issue when I try to tab (with no value entered into the console) and get this message: "append() takes exactly one argument (0 given)"

Here is the code:

tokens = readline.get_line_buffer().split()
if not tokens or readline.get_line_buffer()[-1] == ' ':
    tokens.append()

I'm using the example provided here because of the traverse function where the depth of the tree isn't an issue: https://www.ironalbatross.net/wiki/index.php5?title=Python_Readline_Completions#Complex_problem_.28Regular_Grammar.29

Was it helpful?

Solution 3

OK I managed to fix it... wasn't sure what value to provide append() when there was no value returned by readline so did this and it worked:

def complete(self,text,state):
try:
    tokens = readline.get_line_buffer().split()
    if not tokens or readline.get_line_buffer()[-1] == ' ':
        tokens.append(text)

Thanks guys!

OTHER TIPS

tokens variable is a list, so lists method append really takes exactly one argument.

>>> a = []
>>> a
>>> []
>>> a.append(1)
>>> a
>>> [1]
>>> a.append()
>>> TypeError: append() takes exactly one argument (0 given)
>>> a.append(None)
>>> a
>>> [1, None]
  1. append require exactly one argument

  2. None object can't invoke append function

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top