-2

I have been looking for an answer to this, but no luck yet. I need to have a child class access the value of a property in its parent class.

Here's an example:

class myclass:
    def __init__(self):

        class __firstinternal__:
            def __init__(self):
                self.second = __secondinternal__()

        class __secondinternal__:
            def __init__(self):
                self.var = myclass.getString()

    def getString(self):
        return "something"

    self.internal = __firstinternal__()
    print self.internal.second.var

c = myclass()
Traceback (most recent call last):
  File "./inheritest.py", line 19, in <module>
    c = myclass()
  File "./inheritest.py", line 15, in __init__
    self.internal = __firstinternal__()
  File "./inheritest.py", line 7, in __init__
    self.second = __secondinternal__()
  File "./inheritest.py", line 11, in __init__
    self.anothervar = myclass.getString()
AttributeError: class myclass has no attribute 'getString'

In other words, I need secondinternal to inherit the method getString() from the parent myclass.

I have found how to do this when the class is not a child, but I want to keep the two classes private.

Any ideas?

Thanks in advance!

  • 3
    Your code doesn't make sense. Defining those classes inside the `__init__` method won't do anything; they'll just disappear when the method finishes executing. Is it possible you messed up the indentation in your code here? Are you trying to set classes as attributes of the outer class? Also, why are you using double-underscores? – BrenBarn Jul 22 '12 at 19:27
  • 1
    XY. Describe the problem you're trying to solve, not imagined silly solution. – Cat Plus Plus Jul 22 '12 at 19:30
  • The code is working perfectly as long as the child classes use their own methods. My problem is only when I need to use a method from the parent class. About the double underscores - Well, I thought this is how a child class is supposed to be defined... – user1544357 Jul 22 '12 at 19:32
  • If you run that code, you won't get the error you describe. You'll get a `NameError` saying that `__firstinternal__` is undefined. Also, what do you mean by a "child class"? Defining classes inside other classes is not a usual pattern in Python. – BrenBarn Jul 22 '12 at 19:33
  • The problem I am trying to solve is to have a child class use a method from its parent. If anything is not correctly coded here, I would appreciate your feedback. – user1544357 Jul 22 '12 at 19:34
  • The code is inside a file called inheritest.py. At the end if __name__=='__main__': c = myclass(). When I execute it, the error I posted is the one I get. – user1544357 Jul 22 '12 at 19:37

2 Answers2

2

It's not clear what you mean by "child class" and "parent class". Defining a class inside another class is not a usual Python pattern. You need to explain more about how you're using these terms. Also, your posted code does not give the error you describe, so it seems you're posting different code than you're running.

The best answer I can give based on your code is, "You can't do what you're trying to do." If you define __firstinternal__ inside the __init__ method of myclass, it has no reference to myclass, so it can't access it or any of its methods. myclass does not exist until after all its methods have been defined, so nothing inside the methods can access other parts of the class definition at class-definition time. You can access it at runtime via myclass.getString(), which you say doesn't work, but as mentioned, this is meaningless since the error you give isn't what the code actually does.

It may be that you should be using inheritance rather than defining classes inside each other, but that's just a wild guess.

You should read the Python tutorial to understand how to use classes in Python.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
1

Your code is confused. As best as I can tell, here are two different ways to do something similar to what you want.

class myclass:
    def __init__(self):
        pass

    def getString(self):
        return "something"

class child(myclass):
    def __init__(self):
        self.var = self.getString()

    def someMethod(self):
        return "hello"

...and its output:

>>> c = child()
>>> print c.var
something

Here, I define a parent class myclass and make child inherit myclass. It can then call getString like normal, because it inherited it.

If you need to keep your class private (which isn't a common idiom in python), here's one way to do it:

class myclass2:
    def __init__(self):
        class temp(myclass2): # inherits myclass
            def __init__(self):
                self.var = self.getString()

        self.__privateClass = temp 

    def getString(self):
        return "something else"

    def test(self):
        temp = self.__privateClass()
        print temp.var

And the output:

>>> m = myclass2()
>>> m.test()

...although that method is a bit silly.

Also note that double underscores are used to mark something as being private, not as a child class. (Also note that the concept of 'private variables' is different in python then in other languages. More info)

Community
  • 1
  • 1
Michael0x2a
  • 58,192
  • 30
  • 175
  • 224