Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Need help with syntax on inheritance.

Reply
Thread Tools

Need help with syntax on inheritance.

 
 
SpreadTooThin
Guest
Posts: n/a
 
      10-04-2006
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?

 
Reply With Quote
 
 
 
 
jordan.nick@gmail.com
Guest
Posts: n/a
 
      10-04-2006

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.

 
Reply With Quote
 
 
 
 
Calvin Spealman
Guest
Posts: n/a
 
      10-04-2006
On 3 Oct 2006 19:09:53 -0700, SpreadTooThin <(E-Mail Removed)> 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.
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      10-04-2006
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
 
Reply With Quote
 
SpreadTooThin
Guest
Posts: n/a
 
      10-04-2006

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


 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      10-04-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Syntax Checker that's better than the normal syntax checker Jacob Grover Ruby 5 07-18-2008 05:07 AM
Syntax error? What syntax error? Assignment fo default values? Mark Richards Perl Misc 3 11-18-2007 05:01 PM
Syntax bug, in 1.8.5? return not (some expr) <-- syntax error vsreturn (not (some expr)) <-- fine Good Night Moon Ruby 9 07-25-2007 04:51 PM
[ANN] SqlStatement 1.0.0 - hide the syntax of SQL behind familiarruby syntax Ken Bloom Ruby 3 10-09-2006 06:46 PM
Syntax highligth with textile: Syntax+RedCloth ? gabriele renzi Ruby 2 12-31-2005 02:44 AM



Advertisments