[Zope] how to initialize base class attributes

Aaron Payne aaron@aaronpayne.com
Sat, 07 Jul 2001 11:56:24 -0500


Hi all,

I have a question.  I've written my first python product for zope.  It 
works until I try to access attributes of a base class.

Background:  Worker is a class with User as a base class.
In Firm.py
...
import Being
...
class Worker  (
     Being.User,
     Acquisition.Implicit,
     Persistent,
     AccessControl.Role.RoleManager,
     OFS.SimpleItem.Item,
     ):
...

The problem occurs when I initialize a worker instance.  The statement 
Being.User.__init__(self, firstname, lastname, email) in the __init__ 
method of the worker class raises a TypeError.  I'm a bit confused since 
this statement works in plain python.

 From Firm.py
     def __init__(self, id, title, rate, firstname, lastname, email):
         Being.User.__init__(self, firstname, lastname, email)
         self.id = id
         self.title = title
         self.rate = rate
         self.hours = 0.0

The question is how do i initialize attributes of a base class in zope?

Error Type: TypeError
Error Value: unbound method must be called with class instance 1st argument

Traceback (innermost last):
   File C:\Program Files\Zope19\lib\python\ZPublisher\Publish.py, line 223, 
in publish_module
   File C:\Program Files\Zope19\lib\python\ZPublisher\Publish.py, line 187, 
in publish
   File C:\Program Files\Zope19\lib\python\Zope\__init__.py, line 221, in 
zpublisher_exception_hook
   File C:\Program Files\Zope19\lib\python\ZPublisher\Publish.py, line 171, 
in publish
   File C:\Program Files\Zope19\lib\python\ZPublisher\mapply.py, line 160, 
in mapply
     (Object: manage_addWorker)
   File C:\Program Files\Zope19\lib\python\ZPublisher\Publish.py, line 112, 
in call_object
     (Object: manage_addWorker)
   File C:\Program Files\Zope19\lib\python\Products\TheTracker\Firm.py, 
line 107, in manage_addWorker
   File C:\Program Files\Zope19\lib\python\Products\TheTracker\Firm.py, 
line 38, in __init__
     (Object: User)
TypeError: (see above)


#this is Firm.py
import Being
import Globals
from Globals import Persistent, Acquisition
import AccessControl
import OFS

class Worker  (
     Being.User,
     Acquisition.Implicit,
     Persistent,
     AccessControl.Role.RoleManager,
     OFS.SimpleItem.Item,
     ):
     "A worker."

     index_html = Globals.HTMLFile("workerDisplay", globals())
     manage_main = Globals.HTMLFile("edit_workerForm", globals())

     __ac_permissions__=(
         ('View management screens', ('manage_main',)),
         ('View', ('')),
         )

     manage_options=(
         {'label':'Properties', 'action':'manage_main'},
         {'label':'View', 'action':''},
         )# + OFS.SimpleItem.SimpleItem.manage_options

     meta_type = 'Worker (test)'

     def __init__(self, id, title, rate, firstname, lastname, email):
         Being.User.__init__(self, firstname, lastname, email)
         self.id = id
         self.title = title
         self.rate = rate
         self.hours = 0.0
         #self.email=email
         #self.firstname=firstname
         #self.lastname=lastname

     def addHours(self, n):
         "Add n hours for a worker."
         self.hours = self.hours + n

     def getHours(self):
         "Returns current hours."
         return self.hours

     def getRate(self):
         "Returns current rate."
         return self.rate

     def calcBill(self):
         "Returns current bill."
         return self.rate*self.hours

     def manage_edit(self, id, title, rate, firstname, lastname, email, 
REQUEST=None):
         "Edit worker"
         self.title = title
         self.rate = rate
         #self.email = email
         #self.firstname = firstname
         #self.lastname = lastname
         #self._p_changed = 1
         if REQUEST is not None:
             return Globals.MessageDialog(
                 title='Edited',
                 message='<strong>%s</strong> has been edited.' % self.id,
                 action ='manage_main',
                 )


Globals.default__class_init__(Worker)

class Client(Being.User):
     "A client."

     def __init__(self, username, firm, firstname, lastname, email):
         Being.User.__init__(self, username, firstname, lastname, email)
         self.client_id = username
         self.firm = firm

class Project:
     "A project."

     def __init__(self, project_id, client_id, name, deadline, quote):
         self.hours = 0
         self.project_id=project_id
         self.client_id=client_id
         self.name=name
         self.deadline=deadline
         self.quote=quote



def manage_addWorker(self, id, title, rate, firstname, lastname, email, 
REQUEST=None):
   """Create a worker and install it in its parent Folder.
   The argument 'self' will be bound to the parent Folder.
   """
   workerinstance = Worker(id, title, rate, firstname, lastname, email)
   self._setObject(id, workerinstance)
   if REQUEST is not None:
     return self.manage_main(self, REQUEST)

manage_addWorkerForm = Globals.HTMLFile('add_workerForm', globals())



#this is Being.py
class User:
     "A generic user."

     def __init__(self, firstname, lastname, email):
         self.firstname = firstname
         self.lastname = lastname
         self.email = email

     def getName(self):
         "Returns full name."
         return self.firstname +' '+ self.lastname

-Aaron