|
![](/i/fill.gif) |
Warp wrote:
> Darren New <dne### [at] san rr com> wrote:
>> He's saying that putting "private:" before the variables in a class
>> declaration is equivalent to naming private variables with an underscore. In
>> the first case, the compiler warns you if you use a private variable by
>> mistake. In the second case, it's obvious from inspection. If you have
>> x._y
>> in your code, you're doing something wrong. :-)
>
> Btw (not flaming here), what would be the naming convention for nested
> classes? Take, for example, this kind of situation, in C++ code:
Python is kind of funky in this respect. Nested classes (or functions, or
etc) don't have access to the private variables of the classes they're
nested in. They can only get locals, globals, or built-ins. A lambda
expression can, but that's not a separate class as such, but just an unnamed
function.
Depending on which naming convention you're talking about, class beta nested
inside class alpha would be alpha.beta (if it's a public class), or
alpha._beta if it's a private class.
In python, classes are values just like strings and lists and such. You can
even instantiate classes without using the normal syntax, just by invoking
the appropriate constructor for type "class". The syntax
class abc:
...
just invokes that constructor and assigns the result to the variable abc
(which creates the variable in that namespace if it doesn't already exist).
So if you nest alpha within beta like this
class beta:
static_one = 1
static_two = 2
def beta_add(self,x,y):
return x + y
class alpha:
root = 27
def alpha_add(self,x,y):
return x + y + root
then you get beta.static_one and beta.static_two and beta.beta_add and
beta.alpha. You also get beta.alpha.root and beta.alpha.alpha_add.
Alpha can't access beta.static_one without using a dot. Beta can access
alpha (as in "new alpha" sort of thing) without a dot, because alpha is a
static variable of class beta.
But the namespaces that routines in alpha can access don't include the
namespace that beta uses, and vice versa.
Hence, to get to alpha from outside of beta there, you need to say beta.alpha.
If you want alpha to be private to beta, so beta is (say) your linked-list
class and alpha is a single link, you use "class _alpha:" instead, which
indicates that beta._alpha is meant to be private to beta.
If you want alpha to be public, you name it "class alpha:" and others refer
to it as beta.alpha.
Does that answer it? I probably gave way more details than you were actually
asking about. :-)
The same naming convention holds for what you'd call "member variables" in
C++, member functions, static variables/functions, nested classes, etc.
Everything gets assigned to a variable. If you want gamma to implement the
same foobar function as delta does, you can just write
gamma.foobar = delta.foobar
and they're both the same function afterwards.
--
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) |