Classes and Functions - General Questions

S

Setash

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!
 
S

Sybren Stuvel

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
 
S

Setash

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!
 
J

John Salerno

Setash said:
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.
 
J

John Salerno

Setash said:
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.
 
B

Bruno Desthuilliers

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.
 
A

Andreas Hartl

Setash said:
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?thread=155514>:

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
 
S

Setash

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!
 
J

John Salerno

John said:
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:
 
S

Sybren Stuvel

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,207
Latest member
Best crypto consultant

Latest Threads

Top