Newbie question: unexpected diagnostic when subclassing
Chris Liechti
cliechti at gmx.net
Tue Jul 30 21:06:45 EDT 2002
"Jeff Melvaine" <jeffm52 at rivernet.com.au> wrote in
news:3d472b34$1 at news.rivernet.com.au:
> I think I must be doing something basically wrong, but ... ???
>
> I'm running Python 2.1 on Windows 98. I create a file x.py in which I
> declare
>
> class x:
> ...
>
> The basic operations of this class work OK.
>
> I then create a file y.py in which I declare
>
> class y(x):
> ...
thats a confusing naming scheme.. usual are words starting with a capital
letter for class names.
> When I try to create an instance of y, I get the diagnostic
>
> TypeError: base is not a class instance
>
> referenced to the definition of class y. In this context, does x really
> have to be a variable whose value is an instance of class x?
no it should be a class or type (on 2.2).
class A:
"""base class"""
class B(A):
"""subclassing from A"""
b = B() #instantiate B
if you place them in separate files, you have to import it:
import a #load a.py
class B(a.A):
...
if you use python 2.2, taking built in types as base class it works too
(well type names are all lower case, but still name the user class with
capitals):
class MyFancyList(list):
def append(self, item):
"""a stringifying append"""
list.append(self, str(item))
def toString(self, delim = ''):
"""make a string out of it"""
return delim.join(self)
ml = MyFancyList()
ml.append("hello")
ml.append(1)
ml.append(2)
ml.append(3)
print ml.toString()
chris
--
Chris <cliechti at gmx.net>
More information about the Python-list
mailing list