attribute error

Steven D'Aprano steve at REMOVETHIScyber.com.au
Thu Sep 29 19:21:59 EDT 2005


On Thu, 29 Sep 2005 15:57:47 -0400, M.N.A.Smadi wrote:

> This has nothing to do with how the argument is passed.  It is prob 
> something wrong with str.pop in my python because when i run python and type
> import os
> import string
> x = '1 2 3'
> x.pop()
> 
> i get the following error
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> AttributeError: 'str' object has no attribute 'pop'


That's because strings don't have a pop method, just like the error says.
Did you read the error? Error messages frequently tell you what the error
is.

Lists have a pop method. You can't pop from a string. If you try
to pop from a string, you will get an error. You tried to pop from a
string, and it gave an error. The error told you what you did wrong:
you tried to pop from a string. Why are you surprised?

Go back to your code. Look at the variable that you are trying to pop
from, and notice that it is a string, just like the error message says.
Now search back through your code until you find the place where you
assign a string to that variable. Don't assign a string to it.

Problem fixed.

Like I said the first time I answered your question, I think your problem
is that you are passing in a single argument string like:

"this is a list of commands"

instead of a real list like:

["this", "is", "a", "list", "of", "commands"]

As I also said the first time I answered this, you may find the split()
string method useful for creating that list. (Hint: if x is a string, you
call x.split() and get back a list.)

For future reference: any time you think you have found a bug in Python,
the chances are about 999,999 in a million that you've found a bug in your
code.


-- 
Steven.




More information about the Python-list mailing list