[Tutor] the class struggle
Rob McGee
i812@iname.com
Thu, 29 Nov 2001 17:09:01 -0600
Hi all, I am a python 2.0 beginner. I'm trying to write a silly little
program. Actually I *did* write it using a function definition, but I
wanted to be object-oriented, so I'm trying to use a class instead. This
isn't working out.
{code}
import random
def myObject(name):
variable = name
anotherOne = anotherValue
if name in someList:
nextVar = this
lastOne = that
else:
nextVar = random.randrange(10, 70)
lastOne = random.randrange(5, 15)
{/code}
That works. I got a complete and working (although not very polished)
program using a function like that. It's worthy of the Useless Python
site, for sure. :)
{code}
import random
class myObject(name):
def __init__(self, name):
variable = name
anotherOne = anotherValue
if name in someList:
nextVar = this
lastOne = that
else:
nextVar = random.randrange(10, 70)
lastOne = random.randrange(5, 15)
{/code}
That doesn't work. "SyntaxError: invalid syntax" on the "if" (caret goes
under the "f".) I tried adding "self." in front of the class instance
variables just for fun. Same thing.
I have read the tutorial and what I could find in the other docs about
classes. I rented Ivan Van Laningham's "24 Hours" book at the local
bookstore / coffee shop (I always buy a cup of coffee :) and thought I
I was beginning to understand.
I guess not.
I tried commenting out the if/else lines and making a little function to
test it out, and now everything after this accursed class definition is
a SyntaxError.
Is there something online with another description of using class
definitions? Or perhaps can you fine folks point me in the right
direction?
BTW, except for classes I think I'm understanding things pretty well. I
am able to make python do what I set out to do.
Thank you,
Rob - /dev/rob0