Structs encapsulate data.

Classes encapsulate behavior and data.

Answer from George V. Reilly on Stack Overflow
🌐
Medium
mayowaobisesan.medium.com › class-or-structs-c54263d200ad
CLASS OR STRUCTS? | by Mayowa Obisesan | Medium
January 11, 2024 - Because just like you know that int will throw error if used in place of strings in your code, your user-defined structs will also help control using invalid types where they are being used. Classes: A class is a blueprint for creating objects (a particular data structure), providing initial values for members of the object called member state or member variables or object attributes, and the implementations of the behaviour of those members called member functions or methods.
🌐
Medium
medium.com › swlh › structures-in-python-ed199411b3e1
Structures in Python. In Python, there is a way where you can… | by Akshay Chavan | The Startup | Medium
September 14, 2020 - In python, can define a structure using a class, where the user does not define any functions in the class.
🌐
Quora
quora.com › Why-is-Python-s-class-not-called-a-struct-It-seems-like-a-struct-with-inheritance
Why is Python’s class not called a struct? It seems like a struct with inheritance. - Quora
Answer (1 of 3): Python classes and structs are very far apart for many uses. It is like comparing traveling to a car - sometimes you travel by car, perhaps you even do it often. But if you travel only by car, you miss a lot of traveling. A struct is a group of values, with members addressed by ...
🌐
Sololearn
sololearn.com › en › Discuss › 1378 › i-can-not-understand-difference-between-class-and-struct
I can not understand difference between Class and Struct.
Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
Find elsewhere
🌐
dbader.org
dbader.org › blog › records-structs-and-data-transfer-objects-in-python
Records, Structs, and Data Transfer Objects in Python – dbader.org
June 6, 2017 - You need to add behavior (methods) to the object: You should write a custom class. Either from scratch or by extending collections.namedtuple or typing.NamedTuple. You need to pack data tightly to serialize it to disk or send it over the network: Time to bust out struct.Struct, this is a great use case for it. If you’re looking for a safe default choice, my general recommendation for implementing a plain record, struct, or data object in Python would be to:
🌐
Shiksha
shiksha.com › home › it & software › it & software articles › programming articles › difference between class and structure
Difference between Class and Structure - Shiksha Online
March 6, 2023 - Ques: Create an employee record that has three members’ names, ages, and salaries. Answer: Also Read: Classes and Object in Python · Also Read: Classes and Objects in C++ Structure is a value type, while classes are reference types.
🌐
Mischavandenburg
mischavandenburg.com › zet › go-struct-python-class
Structs In Go - Similar To Classes In Python? | Mischa van den Burg
April 10, 2023 - Structs can have methods, but they are not contained in the struct definition, like you would see in Python. Methods are defined on types, and the type does not need to be a struct.
🌐
Stack Overflow
stackoverflow.com › questions › 35988 › c-like-structures-in-python
struct - C-like structures in Python - Stack Overflow
Is there a way to conveniently define a C-like structure in Python? I'm tired of writing stuff like: class MyStruct(): def __init__(self, field1, field2, field3): self.field1 = field1 ...
🌐
CompleteEra
completeera.com › struct-vs-class-programming-language-differences
Struct vs Class: Programming Language Differences - CompleteEra
April 27, 2026 - TL;DR: Structs are lightweight, stateless data containers (no methods by default), while classes are object-oriented with state (data) + behavior (methods).
🌐
Kitchin Research Group
kitchingroup.cheme.cmu.edu › blog › 2013 › 02 › 27 › Some-basic-data-structures-in-python
Some basic data structures in python
February 27, 2013 - Python does not exactly have the same thing as a struct in Matlab. You can achieve something like it by defining an empty class and then defining attributes of the class.
🌐
TestMu AI Community
community.testmuai.com › ask a question
More Convenient Way to Define Struct-like Classes in Python - Ask a Question - TestMu AI (formerly LambdaTest) Community
November 11, 2024 - How can I define a python struct similar to C-like structures? I’m tired of writing code like this: class MyStruct(): def init(self, field1, field2, field3): self.field1 = field1 self.field2 = field2 self.field3 = field3 …
🌐
Snarky
snarky.ca › proposing-a-struct-syntax
Proposing a struct syntax for Python
June 8, 2023 - For instance, if you had struct Point1D(x), struct Point2D(x, y) and struct Point3D(x, y, z), you could do type Point = Point1D | Point2D | Point3D and have type checkers recognize that all three cases need to be handled by a match statement (maybe with some PatternExhaustion type or something to denote that fact). To be very specific as to why I think this could be better than dataclasses: Typing is optional (there are still folks who don't want to lean into that and it simply isn't always necessary) Class construction should be faster (which intuitively you would think shouldn't matter, but I have talked to folks where this is an actual concern, especially when startup time is critical)
🌐
Medium
medium.com › @imani.sajad92 › struct-vs-class-b4c89ba88070
Struct vs Class. Advantages and Disadvantages | by Sajad Imani | Medium
February 10, 2025 - Struct is value type and it is copied by value, meaning that a complete replica of the data is created when passed or assigned. Class is reference type and it is copied by reference, meaning that only a reference to the original data is passed ...
🌐
Real Python
realpython.com › python-data-structures
Common Python Data Structures (Guide) – Real Python
August 26, 2020 - If you need to add behavior (methods) to the object, then you should write a custom class, either from scratch, or using the dataclass decorator, or by extending collections.namedtuple or typing.NamedTuple. If you need to pack data tightly to serialize it to disk or to send it over the network, then it’s time to read up on struct.Struct because this is a great use case for it! If you’re looking for a safe default choice, then my general recommendation for implementing a plain record, struct, or data object in Python would be to use collections.namedtuple in Python 2.x and its younger sibling, typing.NamedTuple in Python 3.
🌐
Reddit
reddit.com › r/golang › please eli5: how are structs not like classes and embedding not like inheritance
r/golang on Reddit: Please ELI5: How are structs not like Classes and embedding not like inheritance
July 21, 2021 -

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.

Top answer
1 of 11
14
With inheritance, you are saying that a subclass IS a superclass. For example, if Car inherits from Vehicle, it means that Car IS an Vehicle. With composition, you are saying that a struct HAS another struct. For example, if the struct Cars is composed of a struct Battery, it means that a Car HAS a Battery. This means that: With inheritance, all methods from Vehicle would be inherited by Car. This does not happen with composition - Car does not inherit methods from Battery. With inheritance, a subclass can override methods from the superclass (Car can have a method that "replaces" the one from Vehicle). With composition, here is no override. Some languages support multiple inheritance, and with this you may run into the "diamond problem". Let's say that you have a Photocopier class that inherits from Printer and Scanner, and both Printer and Scanner have a ".turnOn()" method, should the language inherit from Printer or Scanner? With composition, since there is no inheritance, Photocopier would not inherit these methods and would not run into the diamond problem. These are some examples, but I'm sure you can find more. They may look very similar on the surface, but there are fundamental differences.
2 of 11
9
I disagree about classes. "Structs with methods" on them are classes, for a cross-language definition of the term "class". They aren't "classes" for any other language's exact definition of class, but those definitions are only helpful in the context of specific languages. One relevant Go-specific difference is that I had to scare-quote the term "structs with methods on them" because they aren't the only classes. You can put methods on any user-defined type . So they are not uniquely classes, you can put methods on more than just structs. Structs are just, for obvious reasons, the "default" thing that happens to. Composition, however, really isn't inheritance. In practice, the big difference is that an inherited class receives a reference to the full instance, and superclass methods end up with that reference and can call overridden methods as a result. With Go composition, composed-in values do not get a reference to the composing value . This means you really can't do inheritance. In Go, you generally do "inheritance" by declaring an interface that your values may conform to, and simply implementing the various values. If you want to share implementation between those values, you absolutely can, you just have to do it via normal functions or other composition, you don't get to use "inheritance". This doesn't contract what u/tail_exchange , it's just another perspective. That was the theoretical perspective, I spoke in this post about the practical way that manifests in functions.