473,499 Members | 1,589 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Where to save classes? How to access classes?

Hi,

I'm trying to get into the object oriented aspect of Python. If I create
a custom class (in it's own file), how do I access that class in a
function in a different file? In Java there's the notion of a CLASSPATH,
where you can tell the compiler to look for classes. Is there something
similar to this in Python?

Thanks,

Dave
Nov 1 '05 #1
1 1232

David Mitchell wrote:
Hi,

I'm trying to get into the object oriented aspect of Python. If I create
a custom class (in it's own file), how do I access that class in a
function in a different file? In Java there's the notion of a CLASSPATH,
where you can tell the compiler to look for classes. Is there something
similar to this in Python?

Thanks,

Dave


Python is a little different than Java in this respect. Whereas Java
loads code one public class at a time, Python loads it in modules,
where a module is (generally speaking) one source file.

Say you have a file, foo.py:
###############################
class Xyz:
def do_stuff(self):
pass

class Abc:
def do_more_stuff(self):
pass
###############################

Then in another file, inserting the statement "import foo" will execute
foo.py and make its global variables available as part of an object
called "foo". There is no automatic loading by class and package name
like Java does it. Once you've done this, you can access the members
using expressions such as "foo.Xyz" e.g. "foo.Xyz().do_stuff()".

As for the search path: sys.path (the path attribute of the sys module)
is a list of directories which are searched, in order, for modules
specified by "import". By default this is the current directory
followed by a few standard Python library directories; most of the time
you shouldn't need to change it, at least for simple projects.

Hope this helps,

-- David

Nov 1 '05 #2

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

Similar topics

5
5059
by: jn | last post by:
I have a page that shows info from MySQL. It is for an online education site. It uses different classes arranged in a hierarchy. Each lesson is roughly laid out like this: Lesson |-Intro |...
0
1487
by: Bengt Richter | last post by:
We have where syntax in combination with suite expression syntax (bear with me, I think a good synergy will emerge ;-) ...
2
1331
by: danny van elsen | last post by:
hello all, in my application, I want to save the state of a number of classes to disk: what would be the best way to do this? right now, I'm hard coding the fields of the classes, but of course...
2
13074
by: gnv | last post by:
Hi all, I am writing a cross-browser(i.e. 6 and netscape 7.1) javascript program to save an XML file to local file system. I have an xml string like below: var xmlStr = "<?xml version="1.0"...
11
6597
by: Yelena Varshal via AccessMonster.com | last post by:
Hello, I have a problem with one of msaccess.exe API calls that work on my desctop but does not work on the laptop from within MS ACCESS. There is a lot of differences between 2 computers...
10
4230
by: Paul Cheetham | last post by:
Hi, I am developing an application that needs to store some machine-specific settings. The application is going to be published on the network in order to keep the clients on the latest version....
24
5314
by: Paul | last post by:
I am taking over an existing app and having trouble understanding their references. They have encapsulated Pear packages in the directory structure like: /site /site/html/Pear.php...
9
3067
by: nik | last post by:
Hi, I would like to create a class and then save it for re-use later. I have tried to use pickle, but am not sure if that is right. I am sorry, but I am new to python. Basically, I have a...
10
1742
by: mirandacascade | last post by:
Question toward the bottom of this post....background information immediately below. Access 97 SQL Server 2000 Please note: although the subject line uses the word 'bloat', this post is NOT...
0
7132
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7178
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,...
0
7390
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...
0
5475
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4919
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...
0
3103
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...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.