How do these Java concepts translate to Python?
ptmcg at austin.rr.com
Fri Aug 12 00:55:06 EDT 2005
Please look through this example code, and the comments. If I've
misspoken, please anyone correct my errors.
"""A definition of an old style class."""
"""Note that NewStyleClass explicitly inherits from object. This
is what makes it new-style."""
"""Class A inherits from classes B and C. Since they are new-style,
A is new-style, too."""
# this is a class variable of A.
classVar = 0
"""This string documents this routine, which is the initializer
for new instances. __init__ is not typically explicitly
called (except from subclasses), but is automatically called
when creating new instances of class A, as in:
aObj = A(initWithThisValue)
Since initArgs is declared with a default value, it is also
possible to create an object as:
aObj = A()
and __init__ will be invoked with None as the value of
if initArgs is not None:
self.instanceVar = initArgs
self.instanceVar = 0
"""This is a class-level method. Presumably it has something
to do with this class, perhaps as a factory or other
This method is invoked as:
A.staticMethod(100, ['A','B'], 3.14159)
What makes this a static method is the @staticmethod
decorator that precedes the class definition.
(Pre-2.4 code would use the form:
staticMethod = staticmethod(staticMethod)
in place of the @staticmethod decorator.)
"""This is also a class-level method, but is distinct in that
the class is implicitly passed as the first argument,
although the caller does not pass the class in.
This method looks similar to the static method invocation:
But in this case, the variable cls takes the value of the
class used to invoke the method, either A or some subclass
"""By default, this method is assumed to be an instance
method. The first argument in the list is the object's
own reference variable - by convention this is near-
universally named 'self', although some prefer the
variable name '_'. Any variable will do really, such
as 'me', 'this', 'I', etc., but it is the first variable
in the list.
The caller does not explicitly pass this object reference
variable in the calling arg list. Invoking instanceMethod
aVar = A()
"""By the magic of the leading '__' on this method name,
this method is not externally visible. It *can* be
called from derived classes, though, so it can be thought
of as roughly analogous to being a 'protected' method
in C++ or Java (although as I recall, 'protected' in
Java isn't really all that protected).
Leading '__' can also be used to hide instance and class
# Here is how you define a class-level variable of A that is of type A.
# You could use these to predefine special A variables, as in
# simulating an enum, or in creating some common values of a given
# class, such as Color.RED, Color.GREEN, etc.
A.specialA = A("special")
A.unusualA = A("unusual")
"""A subclass of A, used to demonstrate calling a classmethod,
and a hidden method."""
# call a method defined in the superclass
# Invoke some class-methods. The first call will pass the class A
# as the first arg, the second will pass the class G as the first arg.
g = G()
g.tryHidden() # Allowed
g.__hiddenMethod(5,6,7) # Not allowed!
More information about the Python-list