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

Efthymios Iosifides iosifidise at gmail.com
Wed Dec 9 01:12:47 EST 2015


Hello Brad.

If we consider that the class resides in a file name Perceptron.py, which
is in the same directory with the  Test Harness.py, you could do the
following in the latter file:

from Percetron import Perceptronp = Perceptron(2)

Best Regards,

Efthimis




2015-12-09 3:46 GMT+02:00 Brad Stiritz via wingide-users <
wingide-users at wingware.com>:

> 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.
>
> _________________________________________________
> Wing IDE users list
> http://wingware.com/lists/wingide
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://wingware.com/pipermail/wingide-users/attachments/20151209/85964dd6/attachment.html>


More information about the wingide-users mailing list