473,788 Members | 2,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Classes and Functions - General Questions

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!

Oct 18 '06 #1
9 1334
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.C lass1):
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/
Oct 18 '06 #2
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.C lass1):
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!

Oct 18 '06 #3
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.
Oct 18 '06 #4
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.
Oct 18 '06 #5
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.
Oct 18 '06 #6
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(*param s):
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?th read=155514>:

from overloading import overloaded

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

@function.regis ter(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
Oct 18 '06 #7
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!

Oct 18 '06 #8
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 <moduleimport <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.
Oct 19 '06 #9
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/
Oct 19 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
3835
by: David MacQuigg | last post by:
I have what looks like a bug trying to generate new style classes with a factory function. class Animal(object): pass class Mammal(Animal): pass def newAnimal(bases=(Animal,), dict={}): class C(object): pass C.__bases__ = bases dict = 0
1
741
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a managed point-of-view I've noticed that: 1) for each managed and unmanaged C function (not C++ classes) I get a public managed static method (defined on a 'Global Functions' class) in the generated assembly with an export name of the form...
45
3628
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes themselves are an exception to this), and 'bootstrap' your program by instantiating a single application object in main(), would that place any limitations on what you could accomplish with your program? Are there any benefits to doing things that...
2
3055
by: Indiana Epilepsy and Child Neurology | last post by:
Before asking this questions I've spent literally _years_ reading (Meyer, Stroustrup, Holub), googling, asking more general design questions, and just plain thinking about it. I am truly unable to figure out what would be a "proper" OO design (in C++) for this. There may be alternatives to writing my own ODBC handle classes, and I may be interested in them, but I'd like to pursue this particular problem, if for no other reason than to...
1
1393
by: Simon Harris | last post by:
Hi All, I'm new to asp.net (Migrating from 'Classic' ASP) I'm having troubles working out classes, functions etc... Current situation is this: index.aspx displays datalist with links to sites - If no link found in DB, I want to display a message instead. I worked out I need a function to do this
13
1734
by: Simon Dean | last post by:
Hi, I have a couple of questions. If you don't mind. Sorry, I do get a bit wordy at times. First one just throws some thoughts around hoping for answers :-) 1) Anyone got a theory on the usage of PHP Classes rather than an actual technical guide? I've seen loads of things that show how to put together a class, but without actually necessarily saying why you'd want to use a class over say a separate file of functions or explaining:
19
1834
by: dl | last post by:
I'll try to clarify the cryptic subject line. Let's say I have a base class 'GeometricObject' with a virtual method 'double distance (const GeometricObject &) const'. Among the derived classes I have eg 'Segment', representing a segment of a line, and 'Arc', representing an arc of circle. What is the right place to put the code for computing the distance between a Segment and an Arc? It is not specific to the Segment nor to
17
3550
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;" instead? I think declaring a function as "=0" is the same
7
3131
by: ademirzanetti | last post by:
Hi there !!! I would like to listen your opinions about inherit from a STL class like list. For example, do you think it is a good approach if I inherit from list to create something like "myList" as in the example below ? #include "Sector.h" using namespace boost;
0
10364
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.