Need help with syntax on inheritance.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SpreadTooThin

    #1

    Need help with syntax on inheritance.

    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.__i nit__(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?

  • jordan.nick@gmail.com

    #2
    Re: Need help with syntax on inheritance.


    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.__i nit__(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.

    Comment

    • Calvin Spealman

      #3
      Re: Need help with syntax on inheritance.

      On 3 Oct 2006 19:09:53 -0700, SpreadTooThin <bjobrien62@gma il.comwrote:
      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.__i nit__(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(typecod e, mine=something)

      It has less to do with defining the parameters than calling the function.

      Comment

      • Peter Otten

        #4
        Re: Need help with syntax on inheritance.

        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.__i nit__(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.arr ay):
        def __init__(self, typecode, initalizer=(), mine=None):
        array.array.__i nit__(self, typecode, initializer)
        self.mine = mine

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

        # should work
        class Array(array.arr ay):
        def __new__(cls, typecode, initializer=(), mine=None):
        return array.array.__n ew__(cls, typecode, initializer)
        def __init__(self, typecode, initializer=(), mine=None):
        array.array.__i nit__(self, typecode, initializer)
        self.mine = mine

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

        Peter

        Comment

        • SpreadTooThin

          #5
          Re: Need help with syntax on inheritance.


          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.__i nit__(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.arr ay):
          def __init__(self, typecode, initalizer=(), mine=None):
          array.array.__i nit__(self, typecode, initializer)
          self.mine = mine
          >
          However, array.array is a bit harder to subclass:
          >
          # should work
          class Array(array.arr ay):
          def __new__(cls, typecode, initializer=(), mine=None):
          return array.array.__n ew__(cls, typecode, initializer)
          def __init__(self, typecode, initializer=(), mine=None):
          array.array.__i nit__(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

          Comment

          • Peter Otten

            #6
            Re: Need help with syntax on inheritance.

            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:

            Contents: General Python FAQ- General Information- What is Python?, What is the Python Software Foundation?, Are there copyright restrictions on the use of Python?, Why was Python created in the fi...

            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

            Comment

            Working...