Comparing Python and C, Part 10, Structures
Please support our Python advertiser: Programming Forums
![]() |
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:
[php]#include <stdio.h>
struct person {
char *name;
char *sex;
char *slogan;
};
int main(void)
{
struct person emma;
struct person jack;
struct person arnie;
// load the structure ...
emma.name = "Emma Porke";
emma.sex = "female";
emma.slogan = "Vote for Bush and Dick";
jack.name = "Jack Schidd";
jack.sex = "male";
jack.slogan = "Never give a schidd!";
arnie.name = "Arnold Negerschwarz";
arnie.sex = "often";
arnie.slogan = "I know how to spell nukilar!";
// show emma, jack and arnie slogans ...
printf("%s says \"%s\"\n", emma.name, emma.slogan);
printf("%s says \"%s\"\n", jack.name, jack.slogan);
printf("%s says \"%s\"\n", arnie.name, arnie.slogan);
getchar(); // console wait
return 0;
}
[/php]
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:
[php]# this Python class mimics a C structure
class Person(object):
"""__init__() functions as the class constructor"""
def __init__(self, name=None, sex=None, slogan=None):
self.name = name
self.sex = sex
self.slogan = slogan
# initialize and load the class (structure) like this ...
emma = Person("Emma Porke", "female", "Vote for Bush and Dick")
jack = Person("Jack Schidd", "male", "Never give a schidd!")
# ... or like this (looks more like the C example) ...
arnie = Person()
arnie.name = "Arnold Negerschwarz"
arnie.sex = "often"
arnie.slogan = "I know how to spell nukilar!"
# show emma, jack and arnie slogans (similar to C) ...
print '%s says "%s"' % (emma.name, emma.slogan)
print '%s says "%s"' % (jack.name, jack.slogan)
print '%s says "%s"' % (arnie.name, arnie.slogan)
print
# ... show it the Pythonian way (good for large person lists) ...
personList = [emma, jack, arnie]
for pers in personList:
print '%s says "%s"' % (getattr(pers, "name"), getattr(pers, "slogan"))
raw_input() # console wait
[/php]
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!"
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:
[php]#include <stdio.h>
struct person {
char *name;
char *sex;
char *slogan;
};
int main(void)
{
struct person emma;
struct person jack;
struct person arnie;
// load the structure ...
emma.name = "Emma Porke";
emma.sex = "female";
emma.slogan = "Vote for Bush and Dick";
jack.name = "Jack Schidd";
jack.sex = "male";
jack.slogan = "Never give a schidd!";
arnie.name = "Arnold Negerschwarz";
arnie.sex = "often";
arnie.slogan = "I know how to spell nukilar!";
// show emma, jack and arnie slogans ...
printf("%s says \"%s\"\n", emma.name, emma.slogan);
printf("%s says \"%s\"\n", jack.name, jack.slogan);
printf("%s says \"%s\"\n", arnie.name, arnie.slogan);
getchar(); // console wait
return 0;
}
[/php]
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:
[php]# this Python class mimics a C structure
class Person(object):
"""__init__() functions as the class constructor"""
def __init__(self, name=None, sex=None, slogan=None):
self.name = name
self.sex = sex
self.slogan = slogan
# initialize and load the class (structure) like this ...
emma = Person("Emma Porke", "female", "Vote for Bush and Dick")
jack = Person("Jack Schidd", "male", "Never give a schidd!")
# ... or like this (looks more like the C example) ...
arnie = Person()
arnie.name = "Arnold Negerschwarz"
arnie.sex = "often"
arnie.slogan = "I know how to spell nukilar!"
# show emma, jack and arnie slogans (similar to C) ...
print '%s says "%s"' % (emma.name, emma.slogan)
print '%s says "%s"' % (jack.name, jack.slogan)
print '%s says "%s"' % (arnie.name, arnie.slogan)
# ... show it the Pythonian way (good for large person lists) ...
personList = [emma, jack, arnie]
for pers in personList:
print '%s says "%s"' % (getattr(pers, "name"), getattr(pers, "slogan"))
raw_input() # console wait
[/php]
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 happygeek : Oct 29th, 2006 at 7:00 am. Reason: Formatting
May 'the Google' be with you!
![]() |
Similar Threads
Other Threads in the Python Forum
- Python and Multimedia (Python)
- pls help (C++)
- Python speed ! (Python)
- Did I do the windchill index correctly.Due in 18hrs.Is there other ways to write this (Python)
- jpg with Python (Python)
- Comparing Python and C, Part 1, Integer Variables (Python)
- NTFS - what can I do? (Getting Started and Choosing a Distro)
- HELP me as soon as possible ..! (C++)
Other Threads in the Python Forum
- Previous Thread: File Compression
- Next Thread: Having 2 versions of Python
•
•
•
•
Views: 8166 | Replies: 1 | Currently Viewing: 1 (0 members and 1 guests)






Linear Mode