0

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?

4
  • 1
    You're writing the StringIO.StringIO class. Commented Oct 14, 2009 at 13:49
  • Well, first of all you have no class methods in your code. And your code works just fine. So what you are doing wrong it most likely to not give us the actual code. ;) Commented Oct 14, 2009 at 13:54
  • 1
    This isn't ruby: You can't call a method by using "as.output". You must put the parenthesis. Commented Oct 14, 2009 at 14:28
  • 2
    I also doubt that the code posted is the actual code you actually ran. I doubt you did as.output(). The error message is probably from doing AltString.output(). Commented Oct 14, 2009 at 14:42

5 Answers 5

8

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

Comments

1

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

1 Comment

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.
1

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.

1 Comment

I think that the code shown in the question is not the code actually run.
0

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.

2 Comments

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: Please do not provide additional information as a comment on an answer. Please update the question with additional information.
0

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.

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.