Reloading after from adder import * ????

Seymour seymour.morris at gmail.com
Sun Nov 19 17:31:53 CET 2006


John,
Thanks for the reply.  Just as a little further background, I am using
Windows XP and Komodo and just trying to learn about classes - how they
work, what they are used for, etc.  I have been studying "Learning
Python (2nd Ed)" by Mark Lutz and Davis Ascher and have been typing in
some of the exercises from Chapt 23, Advanced Class Topics (I tend to
learn better by re-typing them in).  What I posted was only a partial
snippet of the full solution for the Inheritance code solution given on
page 558 of the book, but really was not relevant to my question -
sorry.  I just made some typos and was wondering if there was an easier
way to clear the Python namespace at the interactive prompt rather than
shutting Komodo down and restarting (really brute force).  I finally
figured out the four step solution that I posted but was still
wondering if there was yet an easier way.

FYI, below is the full solution to the Inheritance class exercise in
the book.  Python is a hobby for me and I am not sure  how of if I will
ever use this stuff, but would like to understand it better.  That
said, if you have any follow up study source recommendations, I would
be interested.
Seymour

class Adder:
    def add(self, x, y):
        print 'not implemented!'
    def __init__(self, start=[]):
        self.data = start
    def __add__(self, other):                # Or in subclasses?
        return self.add(self.data, other)    # Or return type?

class ListAdder(Adder):
    def add(self, x, y):
        return x + y

class DictAdder(Adder):
    def add(self, x, y):
        new = {}
        for k in x.keys(): new[k] = x[k]
        for k in y.keys(): new[k] = y[k]
        return new






John Machin wrote:
> Seymour wrote:
> > I created a module with the DictAdder subclass as follows:
> >
> > class DictAdder(Adder):
> >     def add(self, x, y):
> >         new={}
> >         for k in x.keys(): new[k]=x[k]
> >         for k in y.keys(): new[k]=y[k]
> >         return new
> >
> > At the interactive prompt I then said: from Adder import *
> >
> > After defining an instance of DictAdder as x=DictAdder() and trying to
> > add two dictionaries, I notice a typo in DictAdder.  If I then go back
> > and make a change to the DictAdder subclass I have to go through many
> > steps to add two dictionaries (get the revision into active memory):
> > 1. import Adder
> > 2. Reload(Adder)
> > 3. from Adder import *
> > 4. x=DictAdder()
> >
> > Question: Is there an easier and quicker way to get DictAdder into
> > active memory?????
>
> This is one of the occasions where semicolons can come in handy in
> Python:
>
> import adder; reload(adder); from adder import *
>
> Then depending on your OS and what add-on line editing kit (if any) you
> are using, it should only a very small number of keystrokes to retrieve
> that and execute it.
>
> If you meant "is there a single command?", the answer is no, not AFAIK.
>
> Having said that, you seem to have a strange idea about what classes
> are for -- "self" is not used; having such a class "method" seems
> pointless. All you need to do what you are doing  is a simple function,
> whose body can be simply expressed in terms of dict methods:
>
> def funnyfunc(x, y):
>     """This is called a docstring; try to say what the func is doing
> for you"""
>     new = x.copy()
>     new.update(y)
>     return new
>
> Of course that is probably not what you *want* to be doing; but you
> might need to divulge what the Adder class is doing to get more help.
>
> Hints: unlike some other languages,
>
> (1) You don't need to have 1 module file per class; for several small
> related classes (e.g. a main class and a handful of subclasses) it
> makes a lot of sense to put it all in one module.
>
> (2) To do most simple things, you don't even need to write a class at
> all. For example, you can write a few hundred lines of clear
> intelligible Python with very little must-recite-this-spell overhead to
> do quite complicated data manipulations using only (a) built-in
> functions (b) the methods of built-in types like strings, lists and
> dictionaries (c) functions and classes in built-in modules (d) ditto
> 3rd-party modules. In Python classes usually are things that map onto
> something in the real world or at least your model of that; you write
> them when you need to; classes are not artificial hoops you must jump
> through to do the equivalent of:
>    print 2 + 2
> You may end up using classes to emulate some other programming
> constructs, but not yet.
> Eventually you can write those 3rd party modules which are chock-full
> of classes and other goodies, using them as a guide.
> 
> OK, relax, rant over :-)
> 
> HTH,
> John




More information about the Python-list mailing list