Use a named tuple, which was added to the collections module in the standard library in Python 2.6. It’s also possible to use Raymond Hettinger’s named tuple recipe if you need to support Python 2.4.
It’s nice for your basic example, but also covers a bunch of edge cases you might run into later as well. Your fragment above would be written as:
from collections import namedtuple MyStruct = namedtuple("MyStruct", "field1 field2 field3")
The newly created type can be used like this:
m = MyStruct("foo", "bar", "baz")
You can also use named arguments:
m = MyStruct(field1="foo", field2="bar", field3="baz")
Related Posts:
- C-like structures in Python
- “warning: useless storage class specifier in empty declaration” in struct
- Error: Conversion to non-scalar type requested
- Does Python have a string ‘contains’ substring method?
- Behaviour of increment and decrement operators in Python
- Is there a “not equal” operator in Python?
- IndexError: list index out of range and python
- What is the use of “assert” in Python?
- TypeError: ‘list’ object is not callable while trying to access a list
- What is the Python equivalent for a case/switch statement? [duplicate]
- What is the difference between importing matplotlib and matplotlib.pyplot?
- How to delete a file or folder in Python?
- ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
- Reading an Excel file in python using pandas
- Python’s equivalent of && (logical-and) in an if-statement
- “inconsistent use of tabs and spaces in indentation”
- Referring to the null object in Python
- python .replace() regex [duplicate]
- SyntaxError: “can’t assign to function call”
- Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()
- Writing a list to a file with Python
- Why am I getting “LinAlgError: Singular matrix” from grangercausalitytests?
- if else in a list comprehension
- What is the reason for having ‘//’ in Python?
- How can I make a Python script standalone executable to run without ANY dependency? [duplicate]
- How to urlencode a querystring in Python?
- Python Logic of ListNode in Leetcode
- python exception message capturing
- What is the best project structure for a Python application?
- In Matplotlib, what does the argument mean in fig.add_subplot(111)?
- TypeError: unhashable type: ‘dict’, when dict used as a key for another dict
- Importing files from different folder
- How to unpack pkl file?
- What does numpy.gradient do?
- Is there a Python equivalent to the ‘which’ command
- How to run python script in HTML?
- How to clear the interpreter console?
- SFTP in Python? (platform independent)
- Get unique values from a list in python
- ‘list’ object has no attribute ‘shape’
- What does the ‘b’ character do in front of a string literal?
- How to add multiple values per key in python dictionary
- Proper shebang for Python script
- Pythonic way to combine FOR loop and IF statement
- Python: OverflowError: math range error
- Best way to replace multiple characters in a string?
- Convert base-2 binary number string to int
- mysql_config not found when installing mysqldb python interface
- setup script exited with error: command ‘x86_64-linux-gnu-gcc’ failed with exit status 1
- Python 3: UnboundLocalError: local variable referenced before assignment
- Remove all special characters, punctuation and spaces from string
- How to delete last item in list?
- How to configure logging to syslog in Python?
- Converting binary to decimal integer output
- Don’t understand this SyntaxError: illegal target for annotation
- No module named setuptools
- Using Queue in python
- Whats the difference between ‘for x in list:’ and ‘for x in list[:]:’
- Python: maximum recursion depth exceeded while calling a Python object
- python mpl_toolkits installation issue
- What does bitwise_and operator exactly do in openCV?
- RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility
- How to sort 2d array by row in python?
- Using BeautifulSoup to search HTML for string
- TensorFlow: Blas GEMM launch failed
- Python Weather API
- How do I get time of a Python program’s execution?
- Pandas DataFrame: replace all values in a column, based on condition
- How to create a trie in Python
- How to convert string to datetime format in pandas python?
- TypeError: expected a character buffer object
- How to start from second index for for-loop
- Python: How to check if keys exists and retrieve value from Dictionary in descending priority
- How can I set the ‘backend’ in matplotlib in Python?
- Python: Converting string into decimal number
- Segmentation fault: 11 in OS X
- Removing duplicates from a list of lists
- plotly: TypeError: cannot convert dictionary update sequence element #0 to a sequence
- ‘virtualenv’ is not recognized as an internal or external command, operable program or batch file
- Python Coin Toss
- Should Python class filenames also be camelCased?
- Is it better to use path() or url() in urls.py for django 2.0?
- Where are the ampersand and vertical bar characters used in Python?
- How do you test that a Python function throws an exception?
- Plot a histogram such that the total area of the histogram equals 1
- Python Login Script; Usernames and Passwords in a separate file
- Ignore .pyc files in git repository
- MovieWriter ffmpeg unavailable; trying to use class ‘matplotlib.animation.PillowWriter’ instead
- How can I access global variable inside class in Python
- Sorting arrays in NumPy by column
- How to open html file?
- In Python, is there an elegant way to print a list in a custom format without explicit looping?
- “Python version 2.7 required, which was not found in the registry” error when attempting to install netCDF4 on Windows 8
- Does tkinter have a table widget?
- python numpy machine epsilon
- mport win32ui in python 3.6
- Installing lxml module in python
- Extracting specific selected columns to new DataFrame as a copy
- How to read a CSV file from a URL with Python?
- How to print both strings in a dictionary in Python