tags:

views:

109

answers:

6

If you take the following simple class:

class AltString:

    def __init__(self, str = "", size = 0):
        self._contents = str
        self._size = size
        self._list = [str]

    def append(self, str):
        self._list.append(str)

    def output(self):
        return "".join(self._list)

And I successfully invoke the class instance using:

as = AltString("String1")

as.append("String2")

as.append("String3")

When I then invoke the output function using as.output instead of a string being returned, I get the following instead:

unbound method AltString.output

if I call it using as.output() I get the following error:

TypeError: unbound method output() must be called with
  AltString instance as first argument (got nothing instead)

What I am not doing right?

+8  A: 

as is a bad variable name, it is reserved keyword in Python. don't name your variables like this. once you fix it, everything else will be alright. of course you should be doing:

alt_str.output()

edit: I was able to replicate your error messages when trying to apply output to the class: AltString.output, then: AltString.output(). You should be applying the method to the instance of the class instead.

alt_str = AltString('spam')
alt_str.output()
SilentGhost
+1  A: 

'as' and 'str' are keywords, don't shadow them by defining variables with the same name.

Corey Goldberg
`str` is not a keyword, it's just a built-in type. and while it's a bad practice to shadow the built-in, it wouldn't cause such problems.
SilentGhost
A: 

SilentGhost is correct. Also, self._contents and self._size are superfluous in this example.

Ewan Todd
+1  A: 

Your example is confirmed to work as you expect in python 2.4

>>> from x import *
>>> as = AltString("String1")
>>> as.append("bubu")
>>> 
>>> as.output()
'String1bubu'

In python 2.5 it should also work, but will raise a warning about the use of as, which will become a reserved keyword in python 2.6.

I don't really understand why you obtain such error messages. If you are using python 2.6 it should probably produce a syntax error.

Stefano Borini
I think that the code shown in the question is not the code actually run.
S.Lott
A: 

I ran the following code :

class AltString:

    def __init__(self, str = "", size = 0):
        self._contents = str
        self._size = size
        self._list = [str]

    def append(self, str):
        self._list.append(str)

    def output(self):
        return "".join(self._list)


a = AltString("String1")

a.append("String2")

a.append("String3")


print a.output()

And it worked perfectly. The only flow I can see is that you use "as", which is a reserved keyword.

e-satis
The use of 'as' was a typo, as when I did try to use it, I too got a syntax error (I've been using Python 2.5.4).When I re-tried again using your example, everything seemed perfectly fine. Not sure what I was doing wrong before, but it seems to be fine now. :)
Trevor
@Trevor: Please do not provide additional information as a comment on an answer. Please update the question with additional information.
S.Lott
A: 

Just tried your code in Python 2.6.2 and the line

as = AltString("String1")

doesn't work because "as" is a reserved keyword (see here) but if I use another name it works perfectly.

Keeper