[Tutor] the class struggle
Lloyd Kvam
lkvam@venix.com
Thu, 29 Nov 2001 18:38:09 -0500
The syntax error probably relates to name. You show myobject inheriting
from name and then list name as an argument for the __init__ method.
name would have to be a class for the inheritance. It would probably be
an instance or variable for the argument to init.
I suspect that you really want:
class myObject:
Note that people usually capitalize their classes to help prevent this kind
of confusion.
customer = Customer() # create a customer instance from Customer class.
Rob McGee wrote:
> 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
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
--
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358
voice:
603-443-6155
fax:
801-459-9582