I wrote following python program
#! /usr/bin/python
def checkIndex(key):
if not isinstance(key, (int, long)): raise TypeError
if key<0: raise IndexError
class ArithmeticSequence:
def __init__(self, start=0, step=1):
self.start = start # Store the start value
self.step = step # Store the step value
self.changed = {} # No items have been modified
def __getitem__(self, key):
checkIndex(key)
try: return self.changed[key]
except KeyError:
return self.start + key*self.step
def __setitem__(self, key, value):
checkIndex(key)
self.changed[key] = value
the program is my.py when I do
chmod +x my.py
python my.py
I am back to bash shell here after this step I open a python shell
user@ubuntu:~/python/$ python
Python 2.7.3 (default, Aug 1 2012, 05:14:39)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s=ArithmeticSequence(1,2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'ArithmeticSequence' is not defined
How do I give input to my program and run it because it was saved in vi
Velimir Mlaker
11k4 gold badges48 silver badges60 bronze badges
asked Jun 5, 2013 at 18:26
Registered User
5,37718 gold badges49 silver badges73 bronze badges
3 Answers 3
Put your file my.py in PYTHONPATH then
from my import ArithmeticSequence
s=ArithmeticSequence(1,2)
Sign up to request clarification or add additional context in comments.
Comments
The command that you want to run is:
python -i my.py
That will parse my.py and define the name ArithmeticSequence, and drop you into the Python shell where you can use your objects interactively:
>>> s=ArithmeticSequence(1,2)
>>>
answered Jun 5, 2013 at 19:31
Velimir Mlaker
11k4 gold badges48 silver badges60 bronze badges
7 Comments
Registered User
your method python - scriptname.py does work , I used as some one suggested >>> import my and that also worked well, how ever I wrote another program then $bash]python -i some.py works but doing with >>>import some and then running the interactive shell does not work here is the program pastebin.com/deQszXGJ and to run in interactive shell I just do r=Rectangle() in shell try with your method and method as some one else suggests
Registered User
In case you know the reason as why the other method did not work let me know
Velimir Mlaker
You forgot to add #! to the beginning of that file on pastebin. Here's the fix: pastebin.com/6HqJcB5n
Registered User
hmmm thanks for the fix but I tried the program and the other thing of imporiting as
>>>import some does not work and I get error when I do a r=Rectangle() but the same thing (r=Rectangle) does not give an error when I import by python -i someVelimir Mlaker
If using
import some then you have to use r=some.Rectangle(). |
Well you either have to run this as a program using
if __name__ == 'main':
# Your code goes here. This will run when called from command line.
or if you are in the python interpreter you have to import "my" with:
>>> import my
answered Jun 5, 2013 at 18:30
Stoof
7672 gold badges6 silver badges12 bronze badges
Comments
lang-py
>>> import my; s = my.ArithmeticSequence(1,2).my.py.