|
![](/i/fill.gif) |
Warp wrote:
> The question is: If we are using a naming convention to denote public
> and private data, how do you name 'InnerClass' and its private members?
Got it.
OuterClass
OuterClass._private_static_of_OC
OuterClass.public_static_of_OC
OuterClass._InnerClass
OuterClass._InnerClass.used_by_outerclass
OuterClass._InnerClass._Used_only_by_innerclass
Does that clarify?
OuterClass could invoke
x = _InnerClass.used_by_outerclass
The trick is you shouldn't write "._" as part of a reference. :-)
> How how do you name the members of this '_InnerClass'? All of its
> members, even those in the public interface, are part of the private
> implementation of 'OuterClass' and thus shouldn't be used from outside,
> so do you put underscores in all the members, even public ones?
Oh, no. InnerClass isn't really "part" of outer class. It's just a class
that happens to be created while creating InnerClass and assigned to a
static variable of InnerClass.
Python doesn't have declarations, even of classes and functions and methods.
It has statements that have lumps of code in them and creates an instance of
a function object or an instance of a class object, and then assigns it to
the name you gave the statement. So there's no real relationship between
InnerClass and OuterClass, any more than you have a relationship between a
sphere object and the texture on the sphere. There's a relationship there,
but it's not a relationship having to do with types, but rather with
pointers to instance objects. Kind of like javascript in that respect.
> What about the private members of '_InnerClass'? How do you differentiate
> them from the private members of 'OuterClass'?
The private members of _InnerClass have _ on the front. _InnerClass can't
access OuterClass's private or public variables without going through the
class or instance dot-notation.
InnerClass and OuterClass are two 100% separate and independent classes. You
could get *exactly* the same result by declaring it this way:
class InnerClass:
.....
class OuterClass:
...
_InnerClass = InnerClass
...
(Except of course now InnerClass has a global name also)
A nested class definition says "Create a new instance of type 'class' and
assign it to this name in my namespace." But that doesn't give the nested
class any access that any other class could have, and vice versa. It's just
a different name for a class. InnerClass doesn't have anything that points
to the parent class, so there's no way to get to anything in the parent
class directly from InnerClass. If the parent class has a global name like
"OuterClass", then you can refer to that, of course, but there's nothing
that tells InnerClass it's "part of" outerclass.
--
Darren New, San Diego CA, USA (PST)
My fortune cookie said, "You will soon be
unable to read this, even at arm's length."
Post a reply to this message
|
![](/i/fill.gif) |