Comparing Python and C, Part 10, Structures

Thread Solved
Reply

Join Date: Oct 2004
Posts: 3,856
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 866
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Comparing Python and C, Part 10, Structures

 
0
  #1
Sep 19th, 2005
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:

  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
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,856
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 866
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Comparing Python and C, Part 10, Structures

 
0
  #2
Jan 21st, 2007
You can simplify the Python 'for loop':
  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)
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC