Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Revisions

2 of 2
Added explanation related to "structs" and "classes"
Escualo
  • 42.5k
  • 27
  • 95
  • 139

From the Python Official Documentation:

9.7. Odds and Ends

Sometimes it is useful to have a data type similar to the Pascal "record" or C "struct", bundling together a few named data items. An empty class definition will do nicely:

class Employee:
 pass
john = Employee() # Create an empty employee record
# Fill the fields of the record 
john.name = 'John Doe' 
john.dept = 'computer lab' 
john.salary = 1000

This seems natural and simple: Pythonic. Remember the Zen! "Simple is better than complex" (number 3) and "If the implementation is easy to explain, it may be a good idea" (number 11)

In addition, a struct is nothing but a class with public members (i.e., struct{}; and class{public:}; are the exact same thing (in, say, C++)). Shouldn't you consider this and avoid artificial constructs in your Python program? Python is supposed to be readable, maintainable, and easy to understand.

Escualo
  • 42.5k
  • 27
  • 95
  • 139

AltStyle によって変換されたページ (->オリジナル) /