Re: [Zope-dev] Proper Use of __init__ inside Zope Products?

2002-12-13 Thread Dieter Maurer
Jeff Rush writes:
  Can a Zope-internals guru provide some enlightenment regarding the
  mysteries of __init__?
Are there mysteries?

I do not think so. __init__ behaves in Zope exactly the same
as it does in Python.

It behaves differently than (most) other methods in that
its self is (of course) not acquisition wrapped.

  
  As I understand it, the use of __init__ should be avoided when
  possible, since it isn't invoked (necessarily) when persistent
  objects are reloaded from disk.
This is, as it should be.

__init__ is only called when the object is created.
It would be wrong were it called when the object is loaded into
RAM (as this (logically) does not create the object).

  Therefore Zope tends to do instance
  init within manage_addMYPRODUCT global-to-Zope functions, although
  this doesn't seem to be fully consistent throughout the Zope
  community contributions.
It is bad style to do it this way.

The manage_addMYPRODUCT should not know internals about the
constructed instance.

  The scenario is something like the following:
  
  def manage_addFolder(self, id, title='', ...):
  instance = Folder()
  instance.id = str(id)
  instance.title = title
  self._setObject(id, instance)
  
  class Folder(...):
 # no __init__ method
I do not think this is good style...



Dieter

___
Zope-Dev maillist  -  [EMAIL PROTECTED]
http://lists.zope.org/mailman/listinfo/zope-dev
**  No cross posts or HTML encoding!  **
(Related lists - 
 http://lists.zope.org/mailman/listinfo/zope-announce
 http://lists.zope.org/mailman/listinfo/zope )



[Zope-dev] Proper Use of __init__ inside Zope Products?

2002-12-12 Thread Jeff Rush
Can a Zope-internals guru provide some enlightenment regarding the
mysteries of __init__?  I'm writing some zproducts that subclass the
existing Folder class, and then are themselves subclassed.  What I've
got works, but I'm not sure its _correct_.  As I'm writing a Zope
Best Practices document, I'd like to get it right.

As I understand it, the use of __init__ should be avoided when
possible, since it isn't invoked (necessarily) when persistent
objects are reloaded from disk.  Therefore Zope tends to do instance
init within manage_addMYPRODUCT global-to-Zope functions, although
this doesn't seem to be fully consistent throughout the Zope
community contributions.

The scenario is something like the following:

def manage_addFolder(self, id, title='', ...):
instance = Folder()
instance.id = str(id)
instance.title = title
self._setObject(id, instance)

class Folder(...):
   # no __init__ method


def manage_addLargeFolder(self, id, title='', ...):
instance = LargeFolder()
instance.id = str(id)
instance.title = title
self._setObject(id, instance)

class LargeFolder(Folder):
def __init__(self):
# parent class has no __init__ so don't call it!
#Folder.__init__()

self._tree = OOBTree()


def manage_addSpecialLargeFolder(self, id, title='', ...):
instance = SpecialLargeFolder()
instance.id = str(id)
instance.title = title
self._setObject(id, instance)

class SpecialLargeFolder(LargeFolder):
def __init__(self):
# parent class DOES have an __init__ so call it
LargeFolder.__init__()

self._other = OOBTree()

(A) I reluctantly found it necessary to add an __init__ to my
LargeFolder class because if I init'd self._tree in
manage_addLargeFolder(), then it won't get init'd when
manage_addSpecialLargeFolder() is used to create instances.
manage_addLargeFolder doesn't get invoked in that case.

(B) Currently the Folder class lacks an __init__ so I cannot
call it from LargeFolder.__init__, but if someday ZC decides
to add one, my code will fail to init Folder and break.  Is
this correct?  I wish Python auto-provided a default no-nothing
__init__ for this case so I could always call it.

So the Guiding Rule seems to be initialize those attributes
that are common to all Zope objects, e.g. id and title, within
the factory functions, but init any attributes you introduce
using an __init__ method.  Correct?

-Jeff Rush

___
Zope-Dev maillist  -  [EMAIL PROTECTED]
http://lists.zope.org/mailman/listinfo/zope-dev
**  No cross posts or HTML encoding!  **
(Related lists - 
 http://lists.zope.org/mailman/listinfo/zope-announce
 http://lists.zope.org/mailman/listinfo/zope )



Re: [Zope-dev] Proper Use of __init__ inside Zope Products?

2002-12-12 Thread Lennart Regebro
From: Jeff Rush [EMAIL PROTECTED]
 As I understand it, the use of __init__ should be avoided when
 possible, since it isn't invoked (necessarily) when persistent
 objects are reloaded from disk.

I'm not sure why that would matter.
I'm using __init__ all the time, never had one single problem.

Best Regards

Lennart Regebro
Torped Strategi och Kommunikation AB

___
Zope-Dev maillist  -  [EMAIL PROTECTED]
http://lists.zope.org/mailman/listinfo/zope-dev
**  No cross posts or HTML encoding!  **
(Related lists - 
 http://lists.zope.org/mailman/listinfo/zope-announce
 http://lists.zope.org/mailman/listinfo/zope )