Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Customize class creation - python

  1. #1
    Join Date
    Apr 2007
    Location
    Cape Town (SA)
    Beans
    Hidden!

    Customize class creation - python

    I have the following situation:
    Code:
    class Base(object):
       def __init__(self, **kwargs):
          pass
    
    class Child(Base):
       def __init__(self, **kwargs):
          # Do something with kwargs, e.g. filter()
          Base.__init__(self, **kwargs)
    Now I want to be able to create an instance of Child by calling
    Code:
    Base('Child', **kwargs)
    I've had a look at __metaclass__ and __new__, but I can't quite see how to do it. Any suggestions?
    Last edited by aquavitae; January 31st, 2008 at 03:55 PM. Reason: typo

  2. #2
    Join Date
    Apr 2007
    Beans
    14,781

    Re: Customize class creation - python

    <snip />
    Last edited by LaRoza; January 31st, 2008 at 04:15 PM.

  3. #3
    Join Date
    Apr 2007
    Location
    Cape Town (SA)
    Beans
    Hidden!

    Re: Customize class creation - python

    Thanks, but how does this help? I understand how classes and inheritance work - I've programmed a lot in c++, but I haven't got my head around instance creation in python. I've read the python docs, but they don't give any good examples I can work from.

  4. #4
    Join Date
    Apr 2007
    Beans
    14,781

    Re: Customize class creation - python

    Quote Originally Posted by aquavitae View Post
    Thanks, but how does this help? I understand how classes and inheritance work - I've programmed a lot in c++, but I haven't got my head around instance creation in python. I've read the python docs, but they don't give any good examples I can work from.
    Oh, you want to create an instance of the class Child?

    Code:
    instance = Child(args)
    <edit>
    I don't think I understand what you want to do, it may be me, I just got up
    </edit>
    Last edited by LaRoza; January 31st, 2008 at 04:16 PM.

  5. #5
    Join Date
    Apr 2007
    Location
    Cape Town (SA)
    Beans
    Hidden!

    Re: Customize class creation - python

    Maybe I didn't explain properly...

    I want to create an instance of Child like this:
    Code:
    child_instance = Base('Child', answer=42)
    The question is how I code Base to handle this. I also want to keep the interface of Child as clean as possible because I want to be able to add lots of different types of child classes later without having to reproduce lots of code.

    The idea is to use Base as a factory.

  6. #6
    Join Date
    Oct 2006
    Location
    Austin, Texas
    Beans
    2,715

    Re: Customize class creation - python

    Is there any reason why is has to be "Base" that creates the instances? Couldn't you just use a function such as "BaseFactory" and dispatch from there?

  7. #7
    Join Date
    Apr 2006
    Location
    Slovenia
    Beans
    370
    Distro
    Ubuntu Development Release

    Re: Customize class creation - python

    PHP Code:

    class Base(object):
        
    def __new__(clschild,*a, **k):
            for 
    sc in cls.__subclasses__():
                if 
    sc.__name__ == child:
                    return 
    sc
            
    return cls
        
        def __init__
    (self, **kwargs):
            
    pass

    class Child(Base):
        
    def __init__(self, **kwargs):
            
    #Do something with kwargs, e.g. filter()
            
    Base.__init__(self, **kwargs)
            print 
    kwargs

    Base('Child'answer=42)
    print 

    It is still incomplete..

    Why do you need it? Looks like strategy pattern.
    Last edited by Quikee; January 31st, 2008 at 07:44 PM.

  8. #8
    Join Date
    Apr 2007
    Location
    Cape Town (SA)
    Beans
    Hidden!

    Re: Customize class creation - python

    As far as the functionality is concerned, there's no reason why I can't use a function, but its neater to do it this way in my program because I have a number of other related classes (not inherited from Base) which I initialize by
    Code:
    instance = RelatedClass(some_args)
    . Also, because of the way I've designed the program, it makes sense when reading it and typing it to use
    Code:
    Base('specific subclass', arguments_specific_to_subclass)
    Also, I'd just like to know how to do it, just because I'm curious

  9. #9
    Join Date
    Apr 2007
    Location
    Cape Town (SA)
    Beans
    Hidden!

    Re: Customize class creation - python

    Quikee, thanks for the suggestion - its exactly what I wanted! Next question, can I control __init__ from within __new__? What I mean is I that I have all the info for initializing the class in __new__. All that needs to happen before Base.__init__ is called is some process on kwargs. So the process would be this:
    1. Base.__new__ is called
    2. *k is changed as necessary, perhaps by a static method of sc
    3. Base.__init__(self, **k) is called, but not explicitly by sc.__init__
    This is done with the minimum extra or duplicate coding in the subclass, i.e. don't explicitly call Base.__init__. In fact, since I know in advance how kwargs should be adjusted by sc, the code for the subclass could be as simple as:
    Code:
    class SubClass(Base):
        kwargs = {'answer'=42, 'question'='Why?'}
    I can then use dict.update() to modify kwargs in Base.__new__

    I don't know how much of this makes sense, just kinda thinking aloud... If you can make sense of it, any suggestions?

  10. #10
    Join Date
    Jun 2006
    Location
    CT, USA
    Beans
    5,267
    Distro
    Ubuntu 6.10 Edgy

    Re: Customize class creation - python

    To make those tricky design decisions, you may want to learn more about Design_patterns and read couple books. "Headfirst design patterns" was the first book which make sense for me

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •