tags:

views:

680

answers:

5

I'm new to python. I've studied C and I noticed that that the C structure (struct) seemed to have the same task as "class" in python. So what is, conceptually, the difference?

+8  A: 

Aside from numerous technical differences between how they're implemented, they serve roughly the same purpose: the organization of data.

The big difference is that in Python (and other object oriented languages such as C++, Java, or C#), a class can also have functions associated with it that operate only on the instance of the class, whereas in C, a function that wishes to operate on a struct must accept the structure as a parameter in some way, usually by pointer.

I won't delve into the technical differences between the two, as they are fairly significant, but I suggest you look into the concept of Object Oriented Programming.

Randolpho
Thanks for your explanation and the link.
Taurus Olson
Anytime. It's what I'm here for. ... I think
Randolpho
+3  A: 

Classes typically have methods (which are mostly just functions) associated with them, whereas C structs don't. You'll probably want to learn about what object-oriented programming is, if you want to make effective use of classes in Python (or any other object-oriented language, like Java or C++).

Of course, it is possible to use a Python class the same way you'd use a C struct, as a container for data. But that's not done very often.

David Zaslavsky
+5  A: 

Without taking pages and pages to go into the details, think of a C struct as a way to organize data, while a Python (or C++ or Objective-C) "class" is a way to organize not only your data, but the operations for that data. A Python "class," for example, can inherit from other objects, to give you an interator for the data associated with it (or you can write your own interator in the class). There's more to say, I'm sure, but this gets into "what is OOP" pretty quickly, well beyond the scope of this thread.

Craig S
Detailed answer. Thanks.
Taurus Olson
+14  A: 

Structs encapsulate data.

Classes encapsulate behavior and data.

George V. Reilly
Couldn't be more simple.
Taurus Olson
I agree; though when you're doing OOP in C you will often add function pointers to your structs...
Edmund
To give credit where credit is due, I really caught on to classes encapsulating behavior from Allan Shalloway's book, Design Patterns Explained.
George V. Reilly
+5  A: 

One thing that hasn't been mentioned is that C structs are value types, whereas Python classes are reference types.

For example, consider this statement:

var1 = var2

If var1 and var2 were C structs, then that statement would copy the contents of var2 into var1. That's copy by value.

If however var1 and var2 were Python objects, then that statement would make var1 refer to the object that var2 was referring to. (They are like pointers to structs in C.) That's copy by by reference.

The same thing happens when passing arguments to functions (because they have to be copied to get into the function).

dangph