[Tutor] Modularity

Alan Gauld alan.gauld at btinternet.com
Thu Jan 14 03:53:22 EST 2016


On 14/01/16 03:56, kay Cee wrote:

> I'm looking to make individual modules for each class eg. Ball, Paddle,bounds, Game, Physics and Logic.

That's not usually necessary in Python. It's often better
to group several related classes in a single module.
For example your Ball and Paddle and any other display
type things might go together in a module called, say,
pongwidgets.

> Used like this :
> ball = Ball(), paddle = Paddle() and etc...

If you put your Ball class into a module called ball
then you would need to do

import ball

myBall = ball.Ball()

you need to reference the imported module.

Alternatively you could use

from ball import Ball

myBall = Ball()

> Mainly, I get errors that say class is not defined..

It would help if you show us actual code and post
the full error message rather than summarizing things.

> So far I've tried:
> Import class as class,
> From class import*,
> Import class

Note that python is case sensitive so you need lower case.
That may just be your mail tool being too clever though...

The first and third versions are ok, the middle one is frowned on.
It's better to use the

from module import class

style I showed above if you only have (or use) a single
class per module

Post some code and some errors and we will be better
able to help.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list