[Tutor] Major Newbie Here

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Tue Apr 5 22:57:37 CEST 2005



> I'm a cell biologist that is learning python.  I've been following Alan
> Gauld's tutorial online and I've hit a wall.
>
> I'm typing
>
> class Spam:
> 	"""A meat for combining with other foods
>
> 	It can be used with other foods to make interesting meals.
> 	It comes with lots of nutrients and can be cooked using many
> 	different techniques"""
>
>
>  >>> def__init__(self):
>
> SyntaxError: invalid syntax


Hi Gary,

Side note: please subscribe yourself to the mailing list; otherwise, a
silly list admin has to go through and manually allow your posts to get to
the list.  You can find instructions on subscribing here:

    http://mail.python.org/mailman/listinfo/tutor


Welcome aboard!  From what you've shown us so far, it looks like you're
trying to code the class in the interactive interpreter.  You can do this,
but it's probably easier to do this by writing your class in a text file,
and run Python over that file.


I'm guessing that you're entering something like this:

######
class Spam:
    """A meat for combining with other foods
    It can be used with other foods to make interesting meals.
    It comes with lots of nutrients and can be cooked using many
    different techniques"""

def__init__(self):
    ## ... rest of code follows
######


The "class" statement introduces a level of indentation, so you need to
indent all the "methods" that belong to the Spam class.  It's a pure
syntax thing.

Try:

######
class Spam:
    """A meat for combining with other foods
    It can be used with other foods to make interesting meals.
    It comes with lots of nutrients and can be cooked using many
    different techniques"""

    def__init__(self):
        ## ... rest of code follows
######


Tell us if this works out better for you.


By the way, are you using a specialized text editor to work with your
Python programs?  A good text editor makes programming much more pleasant.
Programs like IDLE or PythonWin or the others listed on:

    http://www.python.org/moin/IntegratedDevelopmentEnvironments

can be very helpful.  I've written a somewhat out-of-date IDLE tutorial
here:

    http://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/index.html


Best of wishes to you!



More information about the Tutor mailing list