3

I coded a short linked list program in Python, but without realizing list was a keyword in python, I stored my LinkedList object into list

list = LinkedList()

Running this program worked fine, but now I'm running into issues in another program where I need the list keyword to be used normally, but it still contains a reference to the LinkedList object and not the proper keyword functionality. I have since gone back to my LinkedList class and edited list to be lst instead, but I still have the same error in the class that uses the list keyword. How do I reset the list keyword to its original value?

This is the line that is giving me an error:

df = pd.DataFrame(randn(6,4), index = dates, columns = list("ABCD"))

And this is the error message:

TypeError: 'LinkedList' object is not callable

GreenMatt
  • 18,244
  • 7
  • 53
  • 79
Joel Katz
  • 209
  • 1
  • 2
  • 10
  • can you show the actual error? – depperm Jan 12 '17 at 14:25
  • 9
    Don't use `list` in the first place. Use some other name which is more descriptive. If you removed all instances of `list = #something` then there should be no errors. Show us your stack trace. – This company is turning evil. Jan 12 '17 at 14:26
  • You know... *"I've overridden a keyword"* kind of problems really have an obvious solution... at least for practical applications. Good academical question though! – luk32 Jan 12 '17 at 14:44

2 Answers2

3

It's almost certainly better to stop shadowing builtins, as Kroltan says in his comment, but as the Python quote goes

we are all consenting adults here

so…

>>> list='axe'
>>> __builtins__.list('abc')
['a', 'b', 'c']

Also, for fun, you can convert a literal to its type function using the type function.

>>> list='chuck testa'
>>> type([])('abc')
['a', 'b', 'c']
>>> type(3)(10.5)
10

etc

kojiro
  • 74,557
  • 19
  • 143
  • 201
1

Generally overwriting a builtin is not ideal and you can use del or __builtins__ to fix it.

Using del to reset it:

>>> list = '1,2,3'
>>> list
'1,2,3'
>>> del list
>>> list
<type 'list'>

Using __builtins__.list to reset it.

>>> list = '1,2,3'
>>> list
'1,2,3'
>>> list = __builtins__.list
>>> list
<type 'list'>
Bamcclur
  • 1,949
  • 2
  • 15
  • 19
  • Got a random notification for this question I asked years ago. I don't remember the situation entirely, but I was definitely working in a notebook and not just running a program normally, so this solution would be the answer I was looking for. Thanks! – Joel Katz Sep 27 '22 at 17:41