[wingide-users] How to instantiate a class defined in a separate file?

Brad Stiritz brad.stiritz at gmail.com
Tue Dec 8 20:46:30 EST 2015


Hi everyone, I’m brand-new to Python. I’m having trouble instantiating a
class object. I downloaded code from the following page, and I would like
to step through the code to understand the algorithm:

https://datasciencelab.wordpress.com/2014/01/10/machine-learning-classics-the-perceptron/

I copied and saved the class definition to a file, Perceptron.py, which I
saved in a project that links to Python 3.4.

The web page above says that we simply have to instantiate the class
object, and then run the print() function. I have tried different ways to
accomplish this, but all failed:

1) I saved the instantiation code to a separate file ‘Test_harness.py’ in
the same project folder. I then tried to execute that file, with the
following result:

p = Perceptron(2)

File "w:\Python-work\Perceptron\Test harness.py", line 1, in <module>
    p = Perceptron(2)
NameError: name 'Perceptron' is not defined
Process terminated with an exit code of 1

Question : Does Perceptron have to be imported somehow into the Test
harness?

2) I then tried explicitly importing the class into the same test harness
file, as follows:

import Perceptron
p = Perceptron(2)

Traceback (most recent call last):
  File "w:\Python-work\Perceptron\Test harness.py", line 2, in <module>
    p = Perceptron(2)
TypeError: 'module' object is not callable
Process terminated with an exit code of 1

Question : How do we import a class defined in an external .py file?

Any help appreciated,
Thank you for your consideration.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://wingware.com/pipermail/wingide-users/attachments/20151208/2aaed802/attachment.html>


More information about the wingide-users mailing list