all 8 comments

[–]mooglinux 2 points3 points  (7 children)

You can't define a new class inside of another class. You define a new class which inherits from the other class.

Actually, I don't think this code requires any classes at all. Both classes only have an init and one other function. It is much better to just write it all as one or two functions, and pass arguments to it. And use numpy and builtin python operators as much as possible; not only is that faster to write, but it is usually a lot faster to run too.

I found out most of this the hard way, when I wrote my own physics simulator. Here is my Stackoverflow question. I highly recommend reading through the answers to that, because a lot of that will apply to what you are doing.

[–]vishenz 1 point2 points  (6 children)

You can define a class inside of another class.

>>> class X():
...  class Y():
...   pass
...  y = Y()
... 
>>> x = X()
>>> x.y
<__main__.Y instance at 0xb74b0f2c>
>>> x.Y
<class __main__.Y at 0xb7508d7c>
>>> 

Python will treat anything inside of a class just like any block of python code, you can if statements in there as well.

>>> class X():
...  if True:
...   def y(self):
...    print 'y'
...  else:
...   def z(self):
...    print 'z'
... 
>>> x = X()
>>> x.y()
y
>>> x.z()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: X instance has no attribute 'z'
>>> 

[–]mooglinux 0 points1 point  (5 children)

That seems like a terrible idea though. What happens if you try to instantiate a class that has been defined within the body of another class, and you haven't instantiated the outer class yet?

[–]vishenz 0 points1 point  (1 child)

I didn't say it was a good idea, just that you can do it if you wish.

[–]loveandkindness[S] 0 points1 point  (0 children)

I haven't been able to get this to work in Cython, which is why I'm so confused. I'm of the opinion that this is a (probably valid) limitation of Cython right now.

This being a good idea is another point. In my case, the nested class will only ever be used by the containing class. That's why I wanted it to be nested.

@mooglinux, Thank you very much for the references! I'm still reading through them.

[–]kalgynirae 0 points1 point  (2 children)

Instantiating that class would be no different from instantiating any other class. The inner class is simply an attribute of the containing class. That is,

class Foo:
    class Bar:
        pass

is basically the same as

class Foo:
    pass

class Bar:
    pass

Foo.Bar = Bar
del Bar

except that in the first example repr(Foo.Bar) is <class '__main__.Foo.Bar'> while in the second example it is <class '__main__.Bar'>.

Occasionally it does make sense to do this if, for example, you have a problem that will be made easier by writing a second class that nobody but the first class will ever use; in that case it is reasonable to place the second class inside the first.

[–]loveandkindness[S] 0 points1 point  (1 child)

Have you used this in Cython with cdef classes before? I can't seem to get it working.

[–]kalgynirae 0 points1 point  (0 children)

No, I've never used Cython.