473,409 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

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

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
3 2099
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Nils Grimsmo | last post by:
hi, i'm having some trouble nesting functions. consider the following: def h(): x = 1 def g(): print x # ok, x is taken from h g()
6
by: Andy Baker | last post by:
Hi there, I'm learning Python at the moment and trying to grok the thinking behind it's scoping and nesting rules. I was googling for nested functions and found this Guido quote:...
5
by: Roedy Green | last post by:
I generate code to refer people to bookstores that generates a table without internal borders. You can see an example on http://mindprod.com/environment/kyoto.html I can use this element nested...
4
by: Mr Dyl | last post by:
I'm trying to declare the following friendship and VS.Net 2003 is complaining: template <class T> class Outter { class Inner {...} ... }
8
by: Sean Givan | last post by:
Hi. I'm new to Python, and downloaded a Windows copy a little while ago. I was doing some experiments with nested functions, and ran into something strange. This code: def outer(): val =...
37
by: Tim N. van der Leeuw | last post by:
Hi, The following might be documented somewhere, but it hit me unexpectedly and I couldn't exactly find this in the manual either. Problem is, that I cannot use augmented assignment operators...
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
1
by: Cousson, Benoit | last post by:
Hi, I'd like to be able to use a nested class (C1) from another sibling nested class (C3). This looks very similar to the nested scopes of functions except that it does not work. class...
0
by: Maric Michaud | last post by:
Le Tuesday 12 August 2008 11:29:18 Cousson, Benoit, vous avez écrit : This is a language limitation. This is because nested scope is implemented for python function only since 2.3 allow late...
0
by: Cousson, Benoit | last post by:
This is a language limitation. That was my understanding as well, but I think it is a pity to have that limitation. Don't you think that the same improvement that was done for method nested scope...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.