944,199 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 9815
  • Python RSS
Sep 19th, 2005
0

Comparing Python and C, Part 10, Structures

Expand Post »
Intro

C is using structures to group data, mostly data that belongs together. Here we have some data that a campaign manager might keep for the volunteers. First we take a look at the C code:

  1. #include <stdio.h>
  2.  
  3. struct person {
  4. char *name;
  5. char *sex;
  6. char *slogan;
  7. };
  8.  
  9. int main(void)
  10. {
  11. struct person emma;
  12. struct person jack;
  13. struct person arnie;
  14.  
  15. // load the structure ...
  16. emma.name = "Emma Porke";
  17. emma.sex = "female";
  18. emma.slogan = "Vote for Bush and Dick";
  19.  
  20. jack.name = "Jack Schidd";
  21. jack.sex = "male";
  22. jack.slogan = "Never give a schidd!";
  23.  
  24. arnie.name = "Arnold Negerschwarz";
  25. arnie.sex = "often";
  26. arnie.slogan = "I know how to spell nukilar!";
  27.  
  28. // show emma, jack and arnie slogans ...
  29. printf("%s says \"%s\"\n", emma.name, emma.slogan);
  30. printf("%s says \"%s\"\n", jack.name, jack.slogan);
  31. printf("%s says \"%s\"\n", arnie.name, arnie.slogan);
  32.  
  33. getchar(); // console wait
  34. return 0;
  35. }

No structure?

Well, Python does not have a structure, but then a class is used to group objects together. Let's look at a class that closely mimics the C structure:

python Syntax (Toggle Plain Text)
  1. # this Python class mimics a C structure
  2. class Person(object):
  3. """__init__() functions as the class constructor"""
  4. def __init__(self, name=None, sex=None, slogan=None):
  5. self.name = name
  6. self.sex = sex
  7. self.slogan = slogan
  8.  
  9. # initialize and load the class (structure) like this ...
  10. emma = Person("Emma Porke", "female", "Vote for Bush and Dick")
  11. jack = Person("Jack Schidd", "male", "Never give a schidd!")
  12. # ... or like this (looks more like the C example) ...
  13. arnie = Person()
  14. arnie.name = "Arnold Negerschwarz"
  15. arnie.sex = "often"
  16. arnie.slogan = "I know how to spell nukilar!"
  17.  
  18. # show emma, jack and arnie slogans (similar to C) ...
  19. print '%s says "%s"' % (emma.name, emma.slogan)
  20. print '%s says "%s"' % (jack.name, jack.slogan)
  21. print '%s says "%s"' % (arnie.name, arnie.slogan)
  22.  
  23. print
  24.  
  25. # ... show it the Pythonian way (good for large person lists) ...
  26. personList = [emma, jack, arnie]
  27. for pers in personList:
  28. print '%s says "%s"' % (getattr(pers, "name"), getattr(pers, "slogan"))
  29.  
  30. raw_input() # console wait

Conclusion

Looks like the comparison is pretty close, with some added options of loading and displaying.
Like my uncle Ramon Vegaseat says "There's nothing left to learn the hard way!"
Last edited by vegaseat; Aug 13th, 2009 at 4:25 pm. Reason: Formatting, newer code tags
Similar Threads
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jan 21st, 2007
0

Re: Comparing Python and C, Part 10, Structures

You can simplify the Python 'for loop':
Python Syntax (Toggle Plain Text)
  1. # ... show it the Pythonian way (good for large person lists) ...
  2. personList = [emma, jack, arnie]
  3. for pers in personList:
  4. print '%s says "%s"' % (pers.name, pers.slogan)
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: File Compression
Next Thread in Python Forum Timeline: Having 2 versions of Python





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC