473,386 Members | 1,606 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,386 software developers and data experts.

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

On Wed, Aug 13, 2008 at 11:32 AM, Cousson, Benoit <b-*******@ti.comwrote:
>Defining it as a nested class saves you one line
of code, but IMHO makes the result just a bit more cluttered, while
reducing the elegance of reusing the metaclass.

The whole point of nested class is to avoid polluting the namespace with classes that are only used locally. So the argument about the elegance of reusing is not very valid in that case.
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.
I agree that they are other ways using module to avoid namespace pollution, but in some case it is easier to use nested class instead and keep everything in the same file.
I don't consider it pollution. If you want it considered "private" to
the module, name it with an underscore.
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.

--
Read my blog! I depend on your acceptance of my opinion! I am interesting!
http://techblog.ironfroggy.com/
Follow me if you're into that sort of thing: http://www.twitter.com/ironfroggy
Aug 13 '08 #1
5 3043
On Aug 13, 11:38*am, "Calvin Spealman" <ironfro...@gmail.comwrote:
On Wed, Aug 13, 2008 at 11:32 AM, Cousson, Benoit <b-cous...@ti.comwrote:
Defining it as a nested class saves you one line
of code, but IMHO makes the result just a bit more cluttered, while
reducing the elegance of reusing the metaclass.
The whole point of nested class is to avoid polluting the namespace with classes that are only used locally. So the argument about the elegance ofreusing is not very valid in that case.

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.

I think that's taking it a little too far. It's not unreasonable to
throw small, private use classes into the class definition, like so:

class Someting(object):
class PrivateException(Exception):
pass
And inside function a class definition can make a lot of sense.
Oftentimes I write a quick adaptor class because I want to pass
something to code that expects something else, for instance:

def some_function(lines):
class FileMimicker(object):
def __init__(self):
self.index = 0
def readline(self):
line = lines[self.index]
self.index += 1
return line
function_that_calls_readline(FileMimicker())
(Why would I want to clutter up my module's namespace for that silly
thing?)

So I see no good reason for the compiler to disallow nested class
statements; it's occasionally useful and not a common pitfall.
Carl Banks
Aug 14 '08 #2
On Wed, Aug 13, 2008 at 10:49 PM, Carl Banks <pa************@gmail.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.

I think that's taking it a little too far. It's not unreasonable to
throw small, private use classes into the class definition, like so:

class Someting(object):
class PrivateException(Exception):
pass

And inside function a class definition can make a lot of sense.
Oftentimes I write a quick adaptor class because I want to pass
something to code that expects something else, for instance:

def some_function(lines):
class FileMimicker(object):
def __init__(self):
self.index = 0
def readline(self):
line = lines[self.index]
self.index += 1
return line
function_that_calls_readline(FileMimicker())
(Why would I want to clutter up my module's namespace for that silly
thing?)

So I see no good reason for the compiler to disallow nested class
statements; it's occasionally useful and not a common pitfall.

Carl Banks
I know every rule has its exceptions. I put "don't nest classes" in
with other similar rules I will claim, where I think its safest to say
"Never do this!", because only then will you know that, should you
actually do it at some point, you've got a reason good enough to break
a "rule".

As for the cluttering of the namespace, I don't buy it. Sure there are
cases to be made to reduce the number of names in any scope, but I
don't see a big advantage here. Its still accessible from the module,
just though the class. I also don't see any examples of nested classes
that wouldn't be useful elsewhere, your example included. For that
matter, your example should just being using StringIO.

--
Read my blog! I depend on your acceptance of my opinion! I am interesting!
http://techblog.ironfroggy.com/
Follow me if you're into that sort of thing: http://www.twitter.com/ironfroggy
Aug 14 '08 #3
On Wed, 13 Aug 2008 23:40:50 -0400, Calvin Spealman wrote:
I know every rule has its exceptions. I put "don't nest classes" in with
other similar rules I will claim, where I think its safest to say "Never
do this!", because only then will you know that, should you actually do
it at some point, you've got a reason good enough to break a "rule".
I would put it like this:

"Never nest classes, unless you need to, or to win a bet."

I've never yet needed to nest a class inside a class, but I've used a
"class factory function", a function that returned classes.
--
Steven
Aug 14 '08 #4
Calvin Spealman a écrit :
On Wed, Aug 13, 2008 at 11:32 AM, Cousson, Benoit <b-*******@ti.comwrote:
(snip)
There is no point of nested classes because nested classes _are not_
supported by python.
Depending on the definition of "supported".

(snip)
>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 onced played with the same idea, ie using nested classes as a
declarative approach to XML/HTML generation. It didn't quite work - at
least not well enough to be worth spending more time on the topic - but
at least I can imagine why the OP thinks "true" nested classes would
have help here.

Aug 14 '08 #5
Calvin Spealman a écrit :
(snip)
I know every rule has its exceptions. I put "don't nest classes" in
with other similar rules I will claim, where I think its safest to say
"Never do this!",
I know (from experience) and understand why goto's and globals are
(usually) evil, I know why public attributes can be harmful - in
languages that don't support computed attributes, that is -, well, I can
understand and explain most of the usual and some less known "golden
rules" (law of demeter etc), and even a couple very local or
yet-unwritten ones (monkeypatch anyone ?), but I definitively fail to
see what's your problem with nested classes.
Aug 14 '08 #6

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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.