FAQ
Heya,

I'm sure I've worked out how to to this in the past, but the syntax is
escaping me right now.


class DooHicky(object):
def __init__(self, arg1=None, arg2=None):
pass

class Thingo(DooHicky):
def __init__(self, value=None, *args, **kwargs):
self.value = value
super(Thingo, self).__init__(self, *args, **kwargs)


Basically, I want to pass in an argument to my "Thingo" class which does
not get passed back to the parent constructor.

Any ideas / pointers?

Cheers
-D

Search Discussions

  • Chris Foote at Apr 29, 2008 at 4:20 am

    On Tue, 29 Apr 2008, Darryl Ross wrote:

    I'm sure I've worked out how to to this in the past, but the syntax is
    escaping me right now.


    class DooHicky(object):
    def __init__(self, arg1=None, arg2=None):
    pass

    class Thingo(DooHicky):
    def __init__(self, value=None, *args, **kwargs):
    self.value = value
    super(Thingo, self).__init__(self, *args, **kwargs)


    Basically, I want to pass in an argument to my "Thingo" class which does not
    get passed back to the parent constructor.
    How about:

    class DooHicky(object):
    def __init__(self, arg1=None, arg2=None):
    pass

    class Thingo(DooHicky):
    def __init__(self, **kwargs):
    kwdict = kwargs
    self.value = kwdict['value']
    del kwdict['value']
    super(Thingo, self).__init__(**kwdict)

    you would then need to always use keyword arguments, i.e.:
    Thingo(arg1=5, arg2="gday", value="fred")

    is that ok for you ?

    Chris Foote <chris at inetd.com.au>
    Inetd Pty Ltd T/A HostExpress
    Web: http://www.hostexpress.com.au
    Blog: http://www.hostexpress.com.au/drupal/chris
    Phone: (08) 8410 4566
  • Tim Wegener at Apr 29, 2008 at 4:32 am

    On Tue, 2008-04-29 at 13:19 +0930, Darryl Ross wrote:
    Heya,

    I'm sure I've worked out how to to this in the past, but the syntax is
    escaping me right now.


    class DooHicky(object):
    def __init__(self, arg1=None, arg2=None):
    pass

    class Thingo(DooHicky):
    def __init__(self, value=None, *args, **kwargs):
    self.value = value
    super(Thingo, self).__init__(self, *args, **kwargs)


    Basically, I want to pass in an argument to my "Thingo" class which does
    not get passed back to the parent constructor.

    Any ideas / pointers?
    # If you expect the value argument to be unspecified more often than not:
    class Thingo(DooHicky):
    def __init__(self, *args, **kwargs):
    self.value = kwargs.get('value')
    if self.value is not None:
    del kwargs['value']
    super(Thingo, self).__init__(self, *args, **kwargs)

    # If you expect the value argument to be specified more often than not:
    class Thingo(DooHicky):
    def __init__(self, *args, **kwargs):
    try:
    self.value = kwargs.pop('value')
    except KeyError:
    pass
    super(Thingo, self).__init__(self, *args, **kwargs)

    Both of these will require you to pass value as a keyword argument.

    You might also want to consider avoiding the use of inheritance. I
    can't really comment on whether it is desirable to use inheritance
    here, given the supplied information. Personally I try to avoid
    inheritance unless it is a really nice fit for the situation.

    Tim
  • Darryl Ross at Apr 29, 2008 at 5:01 am

    Tim Wegener wrote:
    # If you expect the value argument to be unspecified more often than not:
    class Thingo(DooHicky):
    def __init__(self, *args, **kwargs):
    self.value = kwargs.get('value')
    if self.value is not None:
    del kwargs['value']
    super(Thingo, self).__init__(self, *args, **kwargs)
    This is pretty much exactly what I came up with.
    You might also want to consider avoiding the use of inheritance. I
    can't really comment on whether it is desirable to use inheritance
    here, given the supplied information. Personally I try to avoid
    inheritance unless it is a really nice fit for the situation.
    In this particular instance, I do actually need the inheritance. I'm
    subclassing a django.newforms.Form.


    Daryl Tester wrote:
    Python version? Under 2.5.1:
    Hmm, good point, I didn't check that. I was testing under 2.4.3 which is
    on my CentOS 5.1 server. On my Ubuntu laptop, which has 2.5.1, my sample
    code works as I was expecting.


    Thanks guys.

    Cheers
    -Darryl
  • Daryl Tester at Apr 29, 2008 at 4:44 am

    Darryl Ross wrote:

    I'm sure I've worked out how to to this in the past, but the syntax is
    escaping me right now.
    Python version? Under 2.5.1:
    t = Thingo()
    t = Thingo(1)
    t = Thingo(value = 11)

    --
    Regards,
    Daryl Tester

    "I hate the Treasury jiggery-pokery / I keep my money in a piggery, locally.
    It's funny but it's okely-dokely" -- "Do it in style", Keating!

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupsapug @
categoriespython
postedApr 29, '08 at 3:49a
activeApr 29, '08 at 5:01a
posts5
users4
websitepython.org

People

Translate

site design / logo © 2018 Grokbase