Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between class (Python) and struct (C)

Tags:

python

c

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?

like image 975
Taurus Olson Avatar asked Oct 23 '25 01:10

Taurus Olson


2 Answers

Structs encapsulate data.

Classes encapsulate behavior and data.

like image 97
George V. Reilly Avatar answered Oct 24 '25 14:10

George V. Reilly


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.

like image 35
Randolpho Avatar answered Oct 24 '25 15:10

Randolpho



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!