Need help with syntax on inheritance.

Discussion in 'Python' started by SpreadTooThin, Oct 4, 2006.

  1. If you are deriving a new class from another class,
    that you must (I assume) know the initializer of the other class.

    So in myClass

    import array
    class myClass(arrary.array):
    def __init__(self, now here I need to put array's constructor
    parameters..., then mine):
    array.array.__init__(self, typecode[, initializer])
    self.mine = mine

    So I'm confused...
    array has a typecode parameter and an optional initiializer...
    So could you help me with the class construction here please?
     
    SpreadTooThin, Oct 4, 2006
    #1
    1. Advertising

  2. SpreadTooThin

    Guest

    SpreadTooThin wrote:
    > If you are deriving a new class from another class,
    > that you must (I assume) know the initializer of the other class.
    >
    > So in myClass
    >
    > import array
    > class myClass(arrary.array):
    > def __init__(self, now here I need to put array's constructor
    > parameters..., then mine):
    > array.array.__init__(self, typecode[, initializer])
    > self.mine = mine
    >
    > So I'm confused...
    > array has a typecode parameter and an optional initiializer...
    > So could you help me with the class construction here please?


    Lookup *args and **kargs in the python reference manual.
     
    , Oct 4, 2006
    #2
    1. Advertising

  3. On 3 Oct 2006 19:09:53 -0700, SpreadTooThin <> wrote:
    > If you are deriving a new class from another class,
    > that you must (I assume) know the initializer of the other class.
    >
    > So in myClass
    >
    > import array
    > class myClass(arrary.array):
    > def __init__(self, now here I need to put array's constructor
    > parameters..., then mine):
    > array.array.__init__(self, typecode[, initializer])
    > self.mine = mine
    >
    > So I'm confused...
    > array has a typecode parameter and an optional initiializer...
    > So could you help me with the class construction here please?


    If you need to take the same parameters as your super-class, and it
    includes optional positional parameters, then simply call with
    keywords to avoid the optional parameter:

    myClass(typecode, mine=something)

    It has less to do with defining the parameters than calling the function.
     
    Calvin Spealman, Oct 4, 2006
    #3
  4. SpreadTooThin

    Peter Otten Guest

    SpreadTooThin wrote:

    > If you are deriving a new class from another class,
    > that you must (I assume) know the initializer of the other class.
    >
    > So in myClass
    >
    > import array
    > class myClass(arrary.array):
    > def __init__(self, now here I need to put array's constructor
    > parameters..., then mine):
    > array.array.__init__(self, typecode[, initializer])
    > self.mine = mine
    >
    > So I'm confused...
    > array has a typecode parameter and an optional initiializer...
    > So could you help me with the class construction here please?


    Normally you would do

    # won't work
    class Array(array.array):
    def __init__(self, typecode, initalizer=(), mine=None):
    array.array.__init__(self, typecode, initializer)
    self.mine = mine

    However, array.array is a bit harder to subclass:

    # should work
    class Array(array.array):
    def __new__(cls, typecode, initializer=(), mine=None):
    return array.array.__new__(cls, typecode, initializer)
    def __init__(self, typecode, initializer=(), mine=None):
    array.array.__init__(self, typecode, initializer)
    self.mine = mine

    See if you can get away by making the array an attribute of your class
    instead.

    Peter
     
    Peter Otten, Oct 4, 2006
    #4
  5. Peter Otten wrote:
    > SpreadTooThin wrote:
    >
    > > If you are deriving a new class from another class,
    > > that you must (I assume) know the initializer of the other class.
    > >
    > > So in myClass
    > >
    > > import array
    > > class myClass(arrary.array):
    > > def __init__(self, now here I need to put array's constructor
    > > parameters..., then mine):
    > > array.array.__init__(self, typecode[, initializer])
    > > self.mine = mine
    > >
    > > So I'm confused...
    > > array has a typecode parameter and an optional initiializer...
    > > So could you help me with the class construction here please?

    >
    > Normally you would do
    >
    > # won't work
    > class Array(array.array):
    > def __init__(self, typecode, initalizer=(), mine=None):
    > array.array.__init__(self, typecode, initializer)
    > self.mine = mine
    >
    > However, array.array is a bit harder to subclass:
    >
    > # should work
    > class Array(array.array):
    > def __new__(cls, typecode, initializer=(), mine=None):
    > return array.array.__new__(cls, typecode, initializer)
    > def __init__(self, typecode, initializer=(), mine=None):
    > array.array.__init__(self, typecode, initializer)
    > self.mine = mine
    >
    > See if you can get away by making the array an attribute of your class
    > instead.
    >


    Thanks.
    the =() syntax indicates what?
    Just slightly off topic here but if Array had a bunch of initializers
    of its own,
    must all the 'optional' parameters be on the right.. ie the last
    parameters?


    > Peter
     
    SpreadTooThin, Oct 4, 2006
    #5
  6. SpreadTooThin

    Peter Otten Guest

    SpreadTooThin wrote:

    > the =() syntax indicates what?


    No special syntax, just an empty tuple as a default parameter.
    In this case I could have used an empty list, too, but I thought I'd spare
    you the dangers of mutable default values as explained here:

    http://www.python.org/doc/faq/general/#id53

    > Just slightly off topic here but if Array had a bunch of initializers
    > of its own, must all the 'optional' parameters be on the right.. ie the
    > last parameters?


    Yes.

    Peter
     
    Peter Otten, Oct 4, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. maxw_cc
    Replies:
    1
    Views:
    3,160
    Martijn van Steenbergen
    Dec 21, 2003
  2. cppsks
    Replies:
    0
    Views:
    833
    cppsks
    Oct 27, 2004
  3. karthikbalaguru
    Replies:
    9
    Views:
    1,055
  4. Daniel Pitts
    Replies:
    27
    Views:
    1,928
    Mike Schilling
    Feb 27, 2008
  5. johnsonlau
    Replies:
    1
    Views:
    783
    Kai-Uwe Bux
    Jul 21, 2008
Loading...

Share This Page