Help needed in OOP-Python

Fredrik Lundh fredrik at pythonware.com
Wed Oct 5 09:29:34 CEST 2005


Toufeeq Hussain wrote:

> My coding is really really bad,that's why I changed the names to more human
> readable form(Module1,2.. etc).

the problem is that when you do that (and post using a tool that's not
smart enough to preserve leading whitespace), anyone who wants to help
will basically have to recreate your program -- and once they've done that,
chances are that they won't get the same error as you do.

consider this:

$ python test.py
Traceback (most recent call last):
  File "test.py", line 1, in ?
    import module3
  File "module3.py", line 4, in ?
    class Option1_Rule1(declaration.Option1):
NameError: name 'declaration' is not defined

oops. looks like you forgot to rename something.  that's easy to fix.

$ python test.py
Traceback (most recent call last):
  File "test.py", line 3, in ?
    Test_Case = module3.Option1_Rule1()
  File "module3.py", line 6, in __init__
    module2.Option1.__init__(self)
  File "module2.py", line 5, in __init__
    module1.OptionClass.__init__('Blah Blah','OR0001','0000','0000')
TypeError: unbound method __init__() must be called with OptionClass instance as
 first argument (got str instance instead)

aha. that sure looks like a bug.  when you call the baseclass init method,
you must pass in the object instance as the first argument.  that's easy to
fix.

$ python test.py
Traceback (most recent call last):
  File "test.py", line 4, in ?
    Test_Case.Option1_constraint()
AttributeError: Option1_Rule1 instance has no attribute 'Option1_constraint'

oops. looks like I got the indentation wrong when I fixed up that module.
that's easy to fix.

$ python test.py
condition satisfied
Traceback (most recent call last):
  File "test.py", line 4, in ?
    Test_Case.Option1_constraint()
  File "module3.py", line 11, in Option1_constraint
    self.FOO_warning.Fire()
AttributeError: Option1_Rule1 instance has no attribute 'FOO_warning'

FOO_warning?  there's no FOO_warning anywhere in the code.

:::

so, after four attempts, I've found four problems, three of which was present
in your posted code, but I still haven't seen the problem you reported:

Traceback (most recent call last):
  File "test_case.py", line 7, in ?
    TH = constraint.Option1_Rule1()
  File "constraint.py", line 13, in __init__
    declaration.Option1.__init__(self)
    TypeError: __init__() takes no arguments (1 given)

which, in itself, looks like you've forgotten the self argument in some init
method somewhere (but the code you posted doesn't have that problem).

if you want to post code, 1) try to reduce the problem to as little code as you
possibly can, and 2) make sure that the code you post really has the problem
you're seeing... (i.e. run it at least once before you post it)

</F>






More information about the Python-list mailing list