Can't seem to start on this

D'Arcy J.M. Cain darcy at druid.net
Thu Jan 3 08:20:31 EST 2013


On Wed, 02 Jan 2013 23:32:33 -0500
Kene Meniru <Kene.Meniru at illom.org> wrote:
> This sounds so simple but being new to python I am finding it hard to
> get started. I want to create a module which I will call "B". There
> will be other modules called "C", "D", etc, which will most likely be
> imported in "B". Then I want the user to import "B" ONLY into another
> file I will call "A" in which commands such as the following will be
> entered:
> 
> snap_size = 10
> LinearMark(name)
> LinearMark.put(name, length, rotation, (x,y,z))

Sounds messy.

> The file "A" allows the user to enter commands that provide global
> variables as well as to use classes provided in modules "C", "D",

OK, "global variables" is the clue that you need to rethink this.  Try
to stay away from global variables as much as possible except for maybe
some simple setup variables within the same file.  Consider something
like this instead.

In file B:

class TopClass(object):
  def __init__(self, snap_size, var1 = None, var2 = None):
    self.snap_size = snap_size
    self.var1 = var1
    if var2 is None: self.var2 = 7
    self.var3 = "GO"
    self.var4 = "Static string"

    *add class methods here*

In file A:

class MyClass(TopClass):
    def __init__(self, var1):
        TopClass.__init__(self, 10, var1, 8)
        self.var3 = "STOP"

x = MyClass(42)
x.var4 = "Not so static after all"

In this (untested) example you create your top class in B and then
subclass it in A.  Notice the different way of setting variables here.
In MyClass we hard code snap_size to 10, we set var1 from the argument
when we instantiate it, var2 is hard coded to 8 but could be left out
if we wanted the default of 7, var3 is overwritten in MyClass and var4
is changed after the class is instantiated.

Hope this gives you some ideas.

-- 
D'Arcy J.M. Cain <darcy at druid.net>         |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.
IM: darcy at Vex.Net



More information about the Python-list mailing list