attribute error

Steve Holden steve at holdenweb.com
Thu Sep 29 23:03:22 CEST 2005


Mike Meyer wrote:
> In <433C473B.9050901 at grads.ece.mcmaster.ca>, M.N.A.Smadi <smadim2 at grads.ece.mcmaster.ca> typed:
> 
>>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'
> 
> 
> The only thing wrong with str.pop is that you're trying to invoke
> it. The interpreter is telling you that string doesn't *have* a pop
> method. The interpreter is right. Strings are immutable, so "pop"
> doesn't make any sense for them.
> 
> 	<mike
> [...]

Just to hammer the point home:

Python 2.4.1 (#1, May 27 2005, 18:02:40)
[GCC 3.3.3 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
  >>> x = '1 2 3'
  >>> x.pop()
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
AttributeError: 'str' object has no attribute 'pop'
  >>> x = [1, 2, 3]
  >>> x.pop()
3
  >>> x.pop()
2
  >>> x
[1]
  >>>

So if you genuinely have a string containing the values, split it onto a 
list first using something like

x = x.split()

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                          www.pycon.org



More information about the Python-list mailing list