By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,915 Members | 1,298 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,915 IT Pros & Developers. It's quick & easy.

RE: Why nested scope rules do not apply to inner Class?

P: n/a
There is no point of nested classes because nested classes _are not_
supported by python. They are simply an artifact of not actively
denying the syntax non-globally. I would fully support a change to the
language to actively forbid a class definition that is not
module-level.
In my case, I'm trying to use a similar approach as XIST's one, meaning
using Python class to model hierarchical data. So clearly nested class is
a very nice and easy understandable way to do that.

I don't find that this is clear in anyway. I can't imagine why you'd
think a nested class is even useful here, rather than an instance with
some understandable attributes. I've seen a lot of places nested
classes are used and not one of them that should be been doing it.

But, on that note, there is a point where a discussion is obviously
not going to resolve with either side changing their minds. This is
obviously such a case.
I don't think so; my original email was mainly a question. I do agree that they are other ways to do what I'm trying to achieve; there are always several ways to solve an issue.
Few days ago, I decided to use nested class because I realized that it was the most convenient way to implement my need.
Since this feature is supported in many languages, I was just surprised that Python did support it only partially, hence my original email.

Now if you say; it is not supported, don't do that, we will deprecate that feature, fine, I will use an alternative solution.

I was just not aware of that "nested class is evil" group in the Python community. I still not understand why, but if it is the BDFL decision...
Regards,
Benoit
Aug 13 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Aug 13, 11:13*am, "Cousson, Benoit" <b-cous...@ti.comwrote:
There is no point of nested classes because nested classes _are not_
supported by python. They are simply an artifact of not actively
denying the syntax non-globally. I would fully support a change to the
language to actively forbid a class definition that is not
module-level.
In my case, I'm trying to use a similar approach as XIST's one, meaning
using Python class to model hierarchical data. So clearly nested class is
a very nice and easy understandable way to do that.
I don't find that this is clear in anyway. I can't imagine why you'd
think a nested class is even useful here, rather than an instance with
some understandable attributes. I've seen a lot of places nested
classes are used and not one of them that should be been doing it.
But, on that note, there is a point where a discussion is obviously
not going to resolve with either side changing their minds. This is
obviously such a case.

I don't think so; my original email was mainly a question. I do agree that they are other ways to do what I'm trying to achieve; there are always several ways to solve an issue.
Few days ago, I decided to use nested class because I realized that it was the most convenient way to implement my need.
Since this feature is supported in many languages, I was just surprised that Python did support it only partially, hence my original email. *

Now if you say; it is not supported, don't do that, we will deprecate that feature, fine, I will use an alternative solution.

I was just not aware of that "nested class is evil" group in the Python community. I still not understand why, but if it is the BDFL decision...
It has nothing to do with nested classes, but rather methods trying to
access other methods:

class X(object):
foo = 42

def bar(self):
print foo
print self.foo
X.foo = 7

When the class is created it *copies* the scope it used, meaning it's
left intact. "print foo" would print 42, rather than the correct and
desired 7 you get from "print self.foo". It is this subtle bug, as
well as "There should be one—and preferably only one—obvious way to do
it", that leads to prohibiting nested access to a class's scope.
Aug 13 '08 #2

P: n/a
Le Wednesday 13 August 2008 21:04:30 Rhamphoryncus, vous avez écrit*:
class X(object):
* * foo = 42

* * def bar(self):
* * * * print foo
* * * * print self.foo
X.foo = 7
Yes this is a problem, function could not bind their free vars to the class
namespace without introducing a subtle incompatibility with actual code.

This is exactly this case which would be a problem (your example raise an
error and is not exactly a "running code"):

G = 1
class A(object) :
G = 0
def f(self) : return G

--
_____________

Maric Michaud
Aug 14 '08 #3

P: n/a
Cousson, Benoit a écrit :
(snip)
Now if you say; it is not supported, don't do that, we will deprecate
that feature, fine, I will use an alternative solution.

I was just not aware of that "nested class is evil" group in the
Python community.
Probably because this group is mostly composed of Mr Calvin Spealman ?
I've been lurking here for quite a few years now, and it's the very
first time I read anything about nested classes being evil....

Aug 14 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.