python can't count!

Terry Reedy tjreedy at udel.edu
Thu Oct 2 23:06:30 EDT 2003


"Dominik Kaspar" <dokaspar at student.ethz.ch> wrote in message
news:62e9c66e.0310020702.7d0e02b7 at posting.google.com...
> i'm wondering if i'm just too stupid for python

Not if you understand the answers you get to the next question ;-)

> or if i've missed something fundamentally important
> of its semantics.

Yes.  instance.method(args) abbreviates
instance-class-or-superclass.method(instance, args).  So

> class Server(threading.Thread):
>    def print_it(n):
>        print n
>     def run(self):
>         self.print_it(34)

translates, in this case, to Server.print_it(self, 34), a call with 2
args.

The abbreviation is possible because instances know what class they
are instances of.  The abbreviation is handy because is saves a few
keystrokes.  It is important because is allows you to call a method on
an instance without specifying (or even necessarily knowing) where in
the class inheritance hierarchy it is defined, and it allows a call to
continue to work even when that place changes.  For instance, after
>s = Server()
,
>s.start()
translates into Thread.start(s).  However, after adding method

  def start(self):
    <add code to log start time>
    Tread.start(self)

to Server, the same s.start() call would instead translate to
Server.start(self) with no change to the method call!

Terry J. Reedy








More information about the Python-list mailing list