nivek
nivek

Reputation: 525

Python nested class definition results in endless recursion... what did I do wrong here?

I am writing a program in Python that opens input files, does some simple text parsing, and outputs. The input comes as an ASCII file with several similarly formatted blocks of text. So, I thought I'd use this as an opportunity to get the hang of defining my own classes.

I have a parent class, pFrame, that I want to inherit the properties of the pandas.DataFrame class. Since my input text files contain two similar (but not identical) types of columnar text, I define two other classes (pFrameA and pFrameB) that each inherit the parent class. At present the child classes just initialize a few variables; later I can define simple helper methods for one, the other, or both classes as needed.

Here's a trimmed down version of the module I wrote to define these classes:

import pandas as pd

class pFrame(pd.DataFrame):
    pass

class pFrameA(pFrame):
    def __init__(self):
        self.units = ('msec', 'psi')
        self.numFormat = (int, float)
        self._gcHeaderStr = "          Time          Cell"


class pFrameB(pFrame):
    def __init__(self):
        self.units = ('in', 'in')
        self.numFormat  = (float, float)
        self._gcHeaderStr = "Disp 1        Disp 2"        

But when I try to test out these class definitions, Python goes into an endless recursion loop:

>>> import pFrameModule
>>> p=pFrameModule.pFrameA()

...
...
    if name in self.columns:
  File "properties.pyx", line 55, in pandas.lib.AxisProperty.__get__ (pandas/lib.c:29248)
  File "/usr/lib/python2.7/dist-packages/pandas/core/frame.py", line 2085, in __getattr__
    if name in self.columns:
  File "properties.pyx", line 55, in pandas.lib.AxisProperty.__get__ (pandas/lib.c:29248)
  File "/usr/lib/python2.7/dist-packages/pandas/core/frame.py", line 2085, in __getattr__
    if name in self.columns:
  File "properties.pyx", line 55, in pandas.lib.AxisProperty.__get__ (pandas/lib.c:29248)
RuntimeError: maximum recursion depth exceeded

Can someone give me a quick pointer to understand what I'm missing?

Upvotes: 1

Views: 176

Answers (1)

Michael0x2a
Michael0x2a

Reputation: 64258

Modifying your code to the following appears to fix it:

import pandas as pd

class pFrame(pd.DataFrame):
    def __init__(self):
        super(pFrame, self).__init__()

class pFrameA(pFrame):
    def __init__(self):
        super(pFrameA, self).__init__()
        self.units = ('msec', 'psi')
        self.numFormat = (int, float)
        self._gcHeaderStr = "          Time          Cell"


class pFrameB(pFrame):
    def __init__(self):
        super(pFrameB, self).__init__()
        self.units = ('in', 'in')
        self.numFormat  = (float, float)
        self._gcHeaderStr = "Disp 1        Disp 2"     


p = pFrameA()
print p._gcHeaderStr    # To prove something is happening

I suspect that by neglecting the call to super in each of your constructors, you weren't properly initializing the DataFrame class, causing it to break under the scenes.

Upvotes: 2

Related Questions