[Tutor] Question About Structs
Nicole Seitz
Nicole.Seitz@urz.uni-hd.de
Fri, 22 Feb 2002 19:31:26 +0100
At 13:39 22.02.2002 -0300, you wrote:
>Hi, Im recently programming in python and I'v one question :
>
>Who is the best form in python to replace what in c is called struct ?
>
>For example in C I have this struct :
>
>struct monitor {
> char *model;
> int number;
>};
>
>My best was to replace this with a DIC :
>
>monitor = {'model':'', 'number':}
>
>This is correct ? There are a best way to do a struct like this in python ?
What about classes?Can't I use a class where I would use a struct in C?
>
>
>Tnks !
>
>Pd: Python doesn't has the case statment ?
You are right, there's no switch statement in Python.
For example, you could use dictionaries as it is shown in "Learning Python":
>>> choice = 'ham'
>>> print {'spam': 1.25, # a dictionary-based 'switch'
... 'ham' : 1.99, # use has_key() test
for default case
... 'eggs' : 0.99,
... 'bacon' : 1.10} [choice]
1.99
I hope someone can explain that.I've just realised that I don't understand
this example at all. It looks nice, though.
For a Newbie like me the alternative solution with an if statement is
better to understand:
>>>if choice == 'spam' :
... print 1.25
... elif choice == 'ham' :
... print 1.99
... elif choice == 'eggs':
... print 0.99
... elif choice == 'bacon' :
... print 1.10
... else:
... print 'Bad choice'
...
1.99
Well, I guess one should prefer the dictionary-based version.Seems to be
more flexible.
Hope this helps .
Nicole
>
>Tnks Again.
>
>Best Regards Hector.
>
>-----------------------------------------------------------------------------
>Fortune :
><Joey> gorgo: *lol*
><gorgo> joey: what's so funny? :)
><Culus> shh, joey is losing all sanity from lack of sleep
><Culus> 'yes joey, very funny'
><Culus> Humor him :>
> -- Seen on #Debian
>-----------------------------------------------------------------------------
>
>_______________________________________________
>Tutor maillist - Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor