Structs encapsulate data.
Classes encapsulate behavior and data.
Answer from George V. Reilly on Stack OverflowStructs encapsulate data.
Classes encapsulate behavior and data.
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.
In perl one can use the 'Class::Struct' library to a large degree as if it were a C-style struct. However the syntax is not the most comfortable.
Is it possible to do the same thing in Python, but using the native 'Class' keyword?
A quick look at the documentation suggests this may work:
Class SettingsStructure:
DataMemberOne = 0
DataMemberTwo = 0Then later in the same programme:
Settings = SettingsStructure() Settings.DataMemberOne = 10 Settings.DataMemberTwo = 20
Is that possible?
To my understanding, in C++, the only major difference between structs and classes is that structs do not allow for any encapsulation - all of their vars and funcs are public.
Why not? Classes are fine for that.
If you want to save some memory, you might also want to use __slots__ so the objects don't have a __dict__. See http://docs.python.org/reference/datamodel.html#slots for details and Usage of __slots__? for some useful information.
For example, a class holding only two values (a and b) could looks like this:
class AB(object):
__slots__ = ('a', 'b')
If you actually want a dict but with obj.item access instead of obj['item'], you could subclass dict and implement __getattr__ and __setattr__ to behave like __getitem__ and __setitem__.
dataclass is now built-in to Python as of Python 3.7!
from dataclasses import dataclass
@dataclass
class EZClass:
name: str='default'
qty: int
Test:
classy = EZClass('cars', 3)
print(classy)
Output:
EZClass(name='cars', qty=3)
Besides for the automatic initialization and __repr__ methods which it generates, it also automatically creates an __eq__ method to make it simple and intuitive to compare two instances of the class.
See PEP 557.
Backported to Python 3.6 using the dataclasses package.
Hello everyone,
I am an aspiring gopher coming in from Python, I am self taught so a lot of concepts are still a bit confusing especially the lower level conceptual things, so I apologize if this is a stupid question.
I'm reading "Learning Go" by Jon Bodner and when reading about structs and embedding it stresses that it's NOT object-oriented and it's NOT inheritance, however when I think about it vis-a-vis how I'm using classes and inheritance in Python they feel almost the same. Could somebody explain like I' 5 how they differ?
Thank you.