Re: factory functions & methods



On Mar 11, 12:52 pm, Piet van Oostrum <p...@xxxxxxxx> wrote:
Aaron Brady <castiro...@xxxxxxxxx> (AB) wrote:
AB> Hello,
AB> I am creating a container.  I have some types which are built to be
AB> members of the container.  The members need to know which container
AB> they are in, as they call methods on it, such as finding other
AB> members.  I want help with the syntax to create the members.
AB> Currently, the container has to be a parameter to the instantiation
AB> methods.  I want the option to create members with attribute syntax
AB> instead.
AB> Currently, I have:
AB> cA= Container( )
AB> obA= SomeType( cA )
AB> obB= OtherType( cA, otherarg )
AB> I want:
AB> cA= Container( )
AB> obA= cA.SomeType( )
AB> obB= cA.OtherType( otherarg )
AB> What are my options?
AB> P.S.  The container and members are C extension types, not pure Python.

You could do something like this (translated to C)

class Container(object):
  def __init__(self):
    self.items = []
  def add(self, item):
    self.items.append(item)
  def SomeType(self):
      newobj = SomeType()
      self.add(newobj)
  def OtherType(self, arg):
      newobj = OtherType(arg)
      self.add(newobj)

class SomeType(object):
  def __init__(self):
      pass

class OtherType(SomeType):
  def __init__(self, arg):
    SomeType.__init__(self)
    self.x = arg

cA = Container()
obA = cA.SomeType()
obB = cA.OtherType(5)
print cA.items

--
Piet van Oostrum <p...@xxxxxxxx>
URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4]
Private email: p...@xxxxxxxxxxxxxx

I like it. It's a combination of andrew's suggestion, and what I've
been considering. What I did was (approximately):

class Container:
def __init__( self ):
self.SomeType= type( 'BoundSomeType', (SomeType,),
{ '__new__': custom_new } )
self.OtherType= type( 'BoundOtherType', (OtherType,),
{ '__new__': custom_new } )

cA = Container()
obA = cA.SomeType()
obB = cA.OtherType(5)

It has the advantage that 'cA.SomeType' is a subclass of SomeType;
specifically, that it responds in kind to SomeType. It's a bit
heavyweight on the consumption of resources. 'custom_new' actually
returns an instance of the base.

I am looking for ways to allow user-defined subclasses.

class CustomType( cA.MemberBase ):
...

obC= CustomType( ) #unusable in other instances

-or-

class CustomType( MemberBase ):
...

obC= cA.CustomType( ) #explicit member or dynamic (Gabriel)?
#of base (Piet) or instance (andrew)?

-or-

class CustomType( MemberBase ):
...

obC= CustomType( ) #disallow "as attribute" creation

Or, some combination of -2- and -1- or -3-.
.



Relevant Pages

  • Re: { }
    ... > Consider a universe consisting of only fruits and boxes. ... > this is the same as a set with three members,. ... Well in that case one should speak about Container theory or box ... seems to be the same band always. ...
    (sci.math)
  • Re: Can a machine be placed in multiple OUs
    ... An object can only reside in one 'container' at a time. ... The mapped network drives are usually handled by ... You might want to look into assigning mapped network drives based on group ... membership (for example, the members of the Marketing group get ...
    (microsoft.public.win2000.active_directory)
  • Re: Add all users to a grouop - Help please
    ... The new group is used by a webmail appliance that can not deal with ... I originally added the members of the 'Site' groups ... script that read all the members of Group1 and added them in this new ...     Else ...
    (microsoft.public.scripting.vbscript)
  • Re: Multiple inheritance - How to call method_x in InheritedBaseB from method_x in InheritedBase
    ... I have a peculiar problem that involves multiple inheritance and method ...    class MyMixin: ... """ Enhanced container that can be moved in response to the user ... allows for optional layout settings specific to each child widget. ...
    (comp.lang.python)
  • Re: Can nested class members access private members of nesting class?
    ... we are saying about class *Container* with some private members. ... > Nested class Node has all of its members as public so that Container ...
    (comp.lang.cpp)