How to instantiate a class defined in a separate file?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to instantiate a class defined in a separate file?

WingIDE - User mailing list
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:


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
Reply | Threaded
Open this post in threaded view
|

Re: How to instantiate a class defined in a separate file?

WingIDE - User mailing list
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 Perceptron
p = Perceptron(2)

Best Regards,
Efthimis



2015-12-09 3:46 GMT+02:00 Brad Stiritz via wingide-users <[hidden email]>:
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:


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


_________________________________________________
Wing IDE users list
http://wingware.com/lists/wingide
Reply | Threaded
Open this post in threaded view
|

Re: How to instantiate a class defined in a separate file?

WingIDE - User mailing list
In reply to this post by WingIDE - User mailing list
Hi Brad,

Not really a wingide question, you might want to read one of the many tutorials on Python, e.g.:
https://www.python.org/about/gettingstarted/

On 12/9/2015 2:46, Brad Stiritz via wingide-users wrote:
...
Question : Does Perceptron have to be imported somehow into the Test harness?
Yes

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?
I would do it like this:

import Perceptron as PM
p = PM.Perceptron(2)

"PM" is the module which can contain multiple classes, functions etc.

You could also do:
import Perceptron
p = Perceptron.Perceptron(2)

Werner


_________________________________________________
Wing IDE users list
http://wingware.com/lists/wingide