How do these Java concepts translate to Python?

Paul McGuire 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.

-- Paul


class OldStyleClass:
    """A definition of an old style class."""
    pass

class NewStyleClass(object):
    """Note that NewStyleClass explicitly inherits from object.  This
       is what makes it new-style."""
    pass

class B(object):
    pass

class C(object):
    pass

class A(B,C):
    """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

    def __init__(self,initArgs=None):
        """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
           initArgs.
        """
        if initArgs is not None:
            self.instanceVar = initArgs
        else:
            self.instanceVar = 0

    @staticmethod
    def staticMethod(a,b,c):
        """This is a class-level method.  Presumably it has something
           to do with this class, perhaps as a factory or other
           utility method.

           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.)
           """
        pass

    @classmethod
    def classMethod(cls,d,e,f):
        """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:
               A.classMethod(5,'XYZZY',[])

           But in this case, the variable cls takes the value of the
           class used to invoke the method, either A or some subclass
           of A.
           """
        print cls,type(cls)

    def instanceMethod(self,g,h,i):
        """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
           looks like:
                aVar = A()
                aVar.instanceMethod(1,2,3)
            """
        pass

    def __hiddenMethod(self,x,y,z):
        """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
           vars, too.
           """
        pass

# 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")

class G(A):
    """A subclass of A, used to demonstrate calling a classmethod,
       and a hidden method."""
    pass

    def tryHidden(self):
        # call a method defined in the superclass
        self.__hiddenMethod(4,5,6)

# 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.
A.classMethod(1,2,3)
G.classMethod(4,5,6)

g = G()
g.tryHidden() # Allowed
g.__hiddenMethod(5,6,7) # Not allowed!




More information about the Python-list mailing list