Classes and Functions - General Questions

Sybren Stuvel sybrenUSE at YOURthirdtower.com.imagination
Wed Oct 18 16:04:15 EDT 2006


Setash enlightened us with:
> 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?

It doesn't matter at all, as long as 'a' is a valid class name. This
works too:

import x

class Classname(x.A):
    do stuff

It's common to start classnames with a captial letter.

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

It needs to be imported first:

class1.py:

    class Class1(object):
        pass

class2.py:
    import class1

    class Class2(class1.Class1):
        pass

> 2) Function overloading - is it possible?

Nope. At least, not that I'm aware of.

> 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

def function(a, b, c=None):
    do things, do other things if c is not None.

Sybren
-- 
Sybren Stüvel
Stüvel IT - http://www.stuvel.eu/



More information about the Python-list mailing list