473,406 Members | 2,849 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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.__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?

Oct 4 '06 #1
5 1175

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 '06 #2
On 3 Oct 2006 19:09:53 -0700, SpreadTooThin <bj********@gmail.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.__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.
Oct 4 '06 #3
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
Oct 4 '06 #4

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
Oct 4 '06 #5
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
Oct 4 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Chris Reedy | last post by:
For everyone - Apologies for the length of this message. If you don't want to look at the long example, you can skip to the end of the message. And for the Python gurus among you, if you can...
4
by: ---- | last post by:
Here are my files (problem follows the code): -------------------------------------------------------------------------- // Group.h #ifndef GROUP_H #define GROUP_H class Group { public:
23
by: Carter Smith | last post by:
http://www.icarusindie.com/Literature/ebooks/ Rather than advocating wasting money on expensive books for beginners, here's my collection of ebooks that have been made freely available on-line...
39
by: clintonG | last post by:
This is not about starting a fight but an observation that seems to be proving itself on its own merit and is therefore simply a point of conjecture. I did not get serious about writing software...
2
by: Edd | last post by:
Hello all, Please consider: class Base { public: int pub; protected: int prot; private: int priv; };
17
by: Student | last post by:
Hi All, I have an assignment for my Programming language project to create a compiler that takes a C++ file as input and translate it into the C file. Here I have to take care of inheritance and...
13
by: Fao | last post by:
Hello, I am having some problems with inheritance. The compiler does not not return any error messages, but when I execute the program, it only allows me to enter the number, but nothing else...
18
by: bsruth | last post by:
I tried for an hour to find some reference to concrete information on why this particular inheritance implementation is a bad idea, but couldn't. So I'm sorry if this has been answered before....
3
by: Zootal | last post by:
I have a question about the syntax involved in inheritance. I have a parent class, and a child class. When I create an instance of the class, I pass to it 3 parameters. Two of them are used by the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.