-4

I've coded a parent class and a nested class. I've already overridden the parent class __str__ method and now I want to do the same on the nested class. But for some reason the __str__ method in the nested class is not getting called. And I can't see the instance variables in PyDev's autocompletion list when coding inside the nested class's str. Code:

class Parent(object):

    def __init__(self):
        self.parent_var = "Parent"

    def __str__(self):
        return self.parent_var


    class Nested(object):

        def __init__(self):
            #Works (of course, why shouldn't it?)
            self.children_var = "Child"

        def __str__(self):
            # This method never called!
            return self.children_var

Something odd is going on. Why can't the __str__ method just work like the __init__ did?


UPDATE:
You guys are right. The code was ok, but my str was trying to do this:

return "Some string" + some_integer

And apparently in Python you need to do str(some_integer) before concatenating stuff (this is actually lame).

Mister Smith
  • 27,417
  • 21
  • 110
  • 193
  • 1
    `str(Parent.Nested())` should work fine, how are you calling it? – Ashwini Chaudhary May 24 '15 at 21:32
  • It's never called *by what*?! Nested classes are rarely the best approach. – jonrsharpe May 24 '15 at 21:32
  • What do you mean called? Try printing the instance of the Nested class. – Malik Brahimi May 24 '15 at 21:32
  • @jonrsharpe Nested classes totally make sense. You python guys have a peculiar view of the world L) – Mister Smith May 24 '15 at 21:33
  • 1
    @MisterSmith Your code is fine. – Malik Brahimi May 24 '15 at 21:35
  • @MisterSmith fine: nested classes are rarely the best approach *in Python*. Happy to take that back if you can provide a more useful example of their use than `Parent` and `Nested`! – jonrsharpe May 24 '15 at 21:35
  • 1
    @MisterSmith: The most common reason to use nested classes in other languages is that those languages don't have first-class functions, or don't have functions at all, and force you to wrap them as methods of useless classes instead. Those reasons don't exist in Python. (Other uses for nested classes do make sense, they just aren't nearly as common.) – abarnert May 24 '15 at 21:37
  • @MisterSmith: Note that Python (unlike some other languages) doesn't attach any special meaning to nested classes, see [Is it a good practice to make nested class in python?](https://stackoverflow.com/q/30376127). Nested classes in Java are used as work-arounds for language limitations that don't exist in Python. – Martijn Pieters May 24 '15 at 21:38
  • 4
    Day 57 and we still don't know how you actually called the `__str__` on the class. [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) – Ashwini Chaudhary May 24 '15 at 21:40
  • 2
    Yeah, strong typing is super lame, it should just quietly let you do the wrong thing and find out later that wasn't what you intended... Please keep the opinions out of your questions. – jonrsharpe May 24 '15 at 21:45
  • @jonrsharpe Java is strongly typed as well and lets you do that. – Mister Smith May 24 '15 at 21:46
  • Interesting, I would consider implicit coercion to be weak typing. Nonetheless, my point stands. Also, I don't see how you couldn't have diagnosed this from the `TypeError`, or why despite having nearly 10k rep you have just *edited an answer/comment into your question* (not to mention asking a question that didn't allow recreation of the issue)! – jonrsharpe May 24 '15 at 21:49
  • @jonrsharpe I needed the `__str__` only for debugging purposes and was not calling print directly, just using eclipse's inspector (who swallowed the error btw). So the answer/comment was actually useful. And you are right, explaining the issue deserved its own question, but I wanted to reward @MalikBrahimi for showing me the way in a one liner. – Mister Smith May 24 '15 at 22:04

1 Answers1

1
print Parent.Nested() # works fine, print calls __str__ resulting in 'Child'
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70