FAQ
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 :)




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


Any thoughts / comments / etc? Just trying to get a solid foundation of
python before going any further.
Thanks!

Search Discussions

  • Sybren Stuvel at Oct 18, 2006 at 8:04 pm

    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/
  • Setash at Oct 18, 2006 at 8:27 pm

    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
    In response to this, would the following also be possible:

    classes.py:

    class Class1
    pass

    class Class2(Class1)
    pass


    or would I still need to call it as:

    class Class2(classes.Class1)
    pass


    Also, I have seen the following syntax used once before, and havent
    found any documentation on it, any comments as to use, where to find
    docs, etc?:

    from module import x as name
    name.function()

    Thanks for the help, you explanation pretty much covered what I wanted
    to know, but also got some more questions!
  • John Salerno at Oct 18, 2006 at 8:59 pm

    Setash wrote:

    Also, I have seen the following syntax used once before, and havent
    found any documentation on it, any comments as to use, where to find
    docs, etc?:

    from module import x as name
    name.function()
    All that does is give you a method for renaming a particularly unruly
    module name to something more manageable, such as this:

    from xml.etree import ElementTree as ET

    Then you can use "ET" to qualify your function calls and such.
  • Sybren Stuvel at Oct 19, 2006 at 2:45 pm

    Setash enlightened us with:
    class1.py:

    class Class1(object):
    pass

    class2.py:
    import class1
    This line imports class1.py and places its contents under the name
    "class1".
    classes.py:

    class Class1
    pass

    class Class2(Class1)
    pass
    That's correct.
    or would I still need to call it as:

    class Class2(classes.Class1)
    pass
    Nope, since the name "classes" is unknown.

    Sybren
    --
    Sybren St??vel
    St??vel IT - http://www.stuvel.eu/
  • John Salerno at Oct 18, 2006 at 8:55 pm

    Setash wrote:

    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?
    If the classes are in the same module, you don't need to do any
    importing or qualification. If they are in separate modules, you need to
    import the necessary module(s) and then you can use its contents.
    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
    I don't know all the details, but you can't do this with Python. One
    alternative is to use an arbitrary number of arguments with the *args
    parameter.
  • John Salerno at Oct 19, 2006 at 2:26 pm

    John Salerno wrote:
    Setash wrote:
    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?
    If the classes are in the same module, you don't need to do any
    importing or qualification. If they are in separate modules, you need to
    import the necessary module(s) and then you can use its contents.
    Quick clarification: even if you import a module, you still need to
    qualify a call to its attributes:

    ----
    import sys

    print sys.version #not 'print version'
    ----


    But you can use the 'from <module> import <attribute>' format to avoid this:


    ----
    from sys import version

    print version
    ----

    But this is very bad to do. The recommendation that I like is to only
    use from/import when you want a module from a package, not when you want
    classes, methods, etc. from a module.
  • Bruno Desthuilliers at Oct 18, 2006 at 9:16 pm

    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.
  • Andreas Hartl at Oct 18, 2006 at 9:38 pm

    Setash schrieb:

    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
    Several ways. The simplest and often most feasible is to create a
    3-argument function with a default value for its last argument

    def function(a, b, c=None):
    if c is None:
    do things you need to do with the third argument
    else:
    do things


    Alternatively, you may use a *params argument that consumes an arbitrary
    number of arguments

    def function(*params):
    if len(params) == 2:
    do 3-argument things
    elif len(params) == 3:
    do 2-argument things

    You can of course mix and match *params with preceding parameters.


    There is a third option - an experimental dynamic function overloading
    module, see BDFL's weblog
    <http://www.artima.com/weblogs/viewpost.jsp?thread5514>:

    from overloading import overloaded

    @overloaded
    def function(a, b):
    do things

    @function.register(object, object, object)
    def function_3(a, b, c):
    do things with 3 parameters


    However, I would not recommend this last solution unless you have a
    really, really weird problem that also heavily depends on the type of
    parameters. Simply stick to the first one, this will be sufficient for
    90% of all cases.
    Andreas
  • Setash at Oct 18, 2006 at 10:41 pm
    Andreas, and everyone else - thank you! I do appreciate the information
    and the quick responses, this single post with <10 replies has
    significantly helped my understanding level.

    Thanks again!

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedOct 18, '06 at 7:42p
activeOct 19, '06 at 2:45p
posts10
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase