1

I am struggling with my code, specifically subclasses. I have a parent class that when initialized will call its subclass to be used as an attribute, in the form of a list of objects. I have seen numerous post where a person forgets to call the __init__() of the parent. My issue is different in that the parent is calling the child class and I do not want to call it.

I am getting the following error:

NameError: name 'bundle' is not defined

I am confused because it is clearly defined as the parent class. Any ideas?

class bundle(object):
"""Main object to hold graphical information"""
    def __init__(self, size,nzone):
        self.size = size
        self.rows = math.sqrt(size)
        self.cols = math.sqrt(size)
        subchans = []
        r = 1
        c = 1 
        for i in range (1,self.size):
            subchans.append(bundle.subbundle(r,c,self.rows,self.cols))
            r += 1            
            if r > self.rows :
                r = 1
                c += 1
    class subbundle(bundle):
        """ Defines geometry to represent subbundle"""
        def  __init__(self, row, col, rows,cols):
4
  • The name bundle is not defined until after the class definition finishes, so subbundle cannot inherit from it and be a nested class. It is not clear from the example why you would want subbundle to be either inherited or nested, though. Commented Dec 2, 2014 at 16:45
  • You cannot call any "subbundle" methods from "bundle" methods. It is the other way : a "subbundle" may call "bundle" methods. Subclassing is for specializing objects, not to engender childs Commented Dec 2, 2014 at 16:46
  • since you are doing the class nested inside of another class it doesnt know what bundle is, i think it would have to be subbundle(self) instead. But what you PROBABLY actually want is to make subbundle(bundle) a class of its own, that is not nested Commented Dec 2, 2014 at 16:46
  • Thanks to all, I am still getting used to OOP and I wanted to encapsulate since these two things are closely related Commented Dec 2, 2014 at 16:53

1 Answer 1

1

When I run your code, I got the error in the following line:

class subbundle(bundle):

That's because you are trying to inherit your subundle class from bundle. I don't know if that's what you actually want to do. Let's assume it is.

When Python tries to parse the .py file, it'll try to figure out the class hierarchy as soon as it sees class bundle. When the interpreter reaches class subbundle(bundle), it doesn't know (yet) what bundle is. Move it to the same level than your class bundle:

class bundle(object):
    def __init__(self, size,nzone):
        self.size = size
        [ . . .]

class subbundle(bundle):
    """ Defines geometry to represent subbundle"""
    def  __init__(self, row, col, rows,cols):
        [ . . . ]

You'll stop seeing your current error and will start seeing a new one: type object 'bundle' has no attribute 'subbundle' That's because it's attempting to treat bundle.subbundle as a method of the class bundle, which is not. You might wanna change the code in the bundle.__init__ to:

for i in range (1,self.size):
    subbundle_instance = subbundle(r, c, self.rows, self.cols)
    subchans.append(subbundle_instance)

PS: It's usually a good practice naming your classes with a capital letter (aka CamelCase) See https://www.python.org/dev/peps/pep-0008#class-names

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.