Re: factory functions & methods
- From: Aaron Brady <castironpi@xxxxxxxxx>
- Date: Wed, 11 Mar 2009 17:38:03 -0700 (PDT)
On Mar 11, 12:52 pm, Piet van Oostrum <p...@xxxxxxxx> wrote:
AB> Hello,Aaron Brady <castiro...@xxxxxxxxx> (AB) wrote:
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-.
.
- References:
- factory functions & methods
- From: Aaron Brady
- Re: factory functions & methods
- From: Piet van Oostrum
- factory functions & methods
- Prev by Date: Re: Can python (CPython) and IPython coexist normally on the same computer ?
- Next by Date: Re: Strange array.array performance
- Previous by thread: Re: factory functions & methods
- Next by thread: How to extract some text?
- Index(es):
Relevant Pages
|