Classes and Functions - General Questions
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Wed Oct 18 17:16:42 EDT 2006
Setash a écrit :
> I've got a tiny bit of coding background, but its not the most
> extensive.
>
> That said, I'm trying to wrap my head around python and have a couple
> questions with classes and functions.
>
> Two notable questions:
>
> 1) Classes. How do you extend classes?
>
> I know its as easy as:
>
> class classname(a)
> do stuff
>
>
> But where does the parent class need to lie? In the same file? Can it
> lie in another .py file in the root directory? Can it simply be
> accessed via an import statement or just plain jane?
>
> To clarify, as it may be worded poorly:
>
> Can my directory structure look like
>
> ..
> /class1.py
> /class2.py
>
> And have class2 inherit class1 without any import statements, or need
> it be imported first?
> Or need class1 and class2 be both declared in the same .py file if
> there is inheritance?
>
> I think thats a bit more clear :)
>
Any object you want to access must be bound to a name in the current
namespace. So you either need to define both classes in the same module
(ie: file), or import the base class. There are some things about this
in the tutorial...
>
>
> 2) Function overloading - is it possible?
>
> Can I have the following code, or something which acts the same in
> python?:
>
>
> def function(a, b)
> do things
>
> def function(a, b, c)
> do things only if I get a third argument
>
There's no proper function overloading builtin Python [1]. But you have
default params:
def function(a, b, c=None):
if c is None:
do things
else:
do things only if I get a third argument
[1] this could be implemented - and is actually implemented (in much
more powerful way) by Philip Eby's dispatch module.
> Any thoughts / comments / etc? Just trying to get a solid foundation of
> python before going any further.
Then you might want to (re ?)read the tutorial and DiveIntoPython.
More information about the Python-list
mailing list