Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

External classes in Python

I'm just beginning Python, and I'd like to use an external RSS class. Where do I put that class and how do I import it? I'd like to eventually be able to share python programs.

like image 840
user29772 Avatar asked Feb 28 '09 20:02

user29772


People also ask

What is a class in Python?

Almost everything in Python is an object, with its properties and methods. A Class is like an object constructor, or a "blueprint" for creating objects. To create a class, use the keyword class: The examples above are classes and objects in their simplest form, and are not really useful in real life applications.

How to define variables outside of a class in Python?

In Python, we can define the variable outside the class, inside the class, and even inside the methods. Let’s see, how to use and access these variables throughout the program. The variables that are defined outside the class can be accessed by any class or any methods in the class by just writing the variable name.

How to create objects using classes in Python?

Classes are just a blueprint for any object and they cannot be used in a program. To create the object defined by the class, we use the constructor of the class to instantiate the object. Due to this, an object is also called an instance of a class.

What is a derived class in Python?

It is a mixture of the class mechanisms found in C++ and Modula-3. Python classes provide all the standard features of Object Oriented Programming: the class inheritance mechanism allows multiple base classes, a derived class can override any methods of its base class or classes, and a method can call the method of a base class with the same name.


3 Answers

About the import statement:

(a good writeup is at http://effbot.org/zone/import-confusion.htm and the python tutorial goes into detail at http://docs.python.org/tutorial/modules.html )

There are two normal ways to import code into a python program.

  1. Modules
  2. Packages

A module is simply a file that ends in .py. In order for python, it must exist on the search path (as defined in sys.path). The search path usually consists of the same directory of the .py that is being run, as well as the python system directories.

Given the following directory structure:

myprogram/main.py
myprogram/rss.py

From main.py, you can "import" the rss classes by running:

import rss
rss.rss_class()

#alternativly you can use:
from rss import rss_class
rss_class()

Packages provide a more structured way to contain larger python programs. They are simply a directory which contains an __init__.py as well as other python files.

As long as the package directory is on sys.path, then it can be used exactly the same as above.


To find your current path, run this:

import sys
print(sys.path)
like image 95
gahooa Avatar answered Oct 08 '22 02:10

gahooa


I don't really like answering so late, but I'm not entirely satisfied with the existing answers.

I'm just beginning Python, and I'd like to use an external RSS class. Where do I put that class and how do I import it?

You put it in a python file, and give the python file an extension of .py . Then you can import a module representing that file, and access the class. Supposing you want to import it, you must put the python file somewhere in your import search path-- you can see this at run-time with sys.path, and possibly the most significant thing to know is that the site-packages (install-specific) and current directory ('') are generally in the import search path. When you have a single homogeneous project, you generally put it in the same directory as your other modules and let them import each other from the same directory.

I'd like to eventually be able to share python programs.

After you have it set up as a standalone file, you can get it set up for distribution using distutils. That way you don't have to worry about where, exactly, it should be installed-- distutils will worry for you. There are many other additional means of distribution as well, many OS-specific-- distutils works for modules, but if you want to distribute a proper program that users are meant to run, other options exist, such as using py2exe for Windows.

As for the modules/packages distinction, well, here it goes. If you've got a whole bunch of classes that you want divided up so that you don't have one big mess of a python file, you can separate it into multiple python files in a directory, and give the directory an __init__.py . The important thing to note is that from Python, there's no difference between a package and any other module. A package is a module, it's just a different way of representing one on the filesystem. Similarly, a module is not just a .py file-- if that were the case, sys would not be a module, since it has no .py file. It's built-in to the interpreter. There are infinitely many ways to represent modules on the filesystem, since you can add import hooks that can create ways other than directories and .py files to represent modules. One could, hypothetically, create an import hook that used spidermonkey to load Javascript files as Python modules.

like image 22
Devin Jeanpierre Avatar answered Oct 08 '22 02:10

Devin Jeanpierre


from [module] import [classname]

Where the module is somewhere on your python path.

like image 28
Andy Hume Avatar answered Oct 08 '22 00:10

Andy Hume