0

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

3 Answers 3

1

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]
Sign up to request clarification or add additional context in comments.

Comments

0
  1. append require exactly one argument

  2. None object can't invoke append function

Comments

0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.