Classes and the command line

Rustom Mody rustompmody at gmail.com
Sun Oct 26 23:05:43 EDT 2014


On Monday, October 27, 2014 8:00:04 AM UTC+5:30, Seymore4Head wrote:
> I am trying to learn classes.
> I am currently using Python 2.7 at the command line.

Why not idle?
And if in general you are at python 3, why 2.7 here?
There are enough factor to learn ( and get confused)!
Please dont add new ones gratuitously!

> If you try to type commands at the command line and make the slightest
> mistake you have to start over.
> I was trying to copy and paste these instructions into the command
> prompt. 
> 
> http://en.wikibooks.org/wiki/Python_Programming/Classes
> >>> class Foo:
> ...     def setx(self, x):
> ...         self.x = x
> ...     def bar(self):
> ...         print self.x
> 
> There is really no way to do that without pasting line by line is
> there and adding deleting spaces?  And if you use spaces and tabs,
> they are not the same.
> 
> http://imgur.com/a/XTkAm

Let me repeat and elaborate what I have already told you --
- you should write mostly expressions at the interpreter prompt
- you should write definitions in the file

Rough distinctions:

1. Things like 1+2, lst.append(3) etc are expressions
2. Things like assignment, if, while are statements
3. class and def statements are special; and called definitions

Notes
a. theres an inclusion 1 'sits inside' 2 'sits inside' 3
b. Its easiest if you try 1 in the interpreter; 3 in the python file
c. 2 is a bit clumsy. Short ones can be tried at the interpreter
   Longer than 3 (max 4) lines should go into files.
   May require a bit of thought how to package a statement into  
   a definition to try out
d. 2 is clumsy in files for a different reason; you will need
   to write prints judiciously
e. Since def itself goes into class, if youve not yet worked out
   a comfortable 'tryin-out' model, stay with defs for now

In short:
For a while people here have been advising you "Use the Interpreter"
Now I will add slightly to that:
Use the interpreter in idle, along with a python file



More information about the Python-list mailing list