1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-16 21:48:02 +00:00

Compare commits

...

3 Commits

Author SHA1 Message Date
nick-w-nick
4898f768d5 Merge 295fe153ad into 36ca71bfb1 2024-11-08 10:30:51 -05:00
jbranchaud
36ca71bfb1 Add Store And Access Immutable Data In A Tuple as a Python TIL 2024-11-08 09:16:45 -06:00
nick-w-nick
295fe153ad added mention of ES6 compatibility
Hello, I've added a small blockquote below the description to indicate that this method of accessing an indefinite number of function arguments has been superseded by the use of the spread operator via rest parameters for ES6+ compatibility.
2022-01-06 11:39:04 -05:00
3 changed files with 53 additions and 1 deletions

View File

@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
_1500 TILs and counting..._
_1501 TILs and counting..._
---
@@ -885,6 +885,7 @@ _1500 TILs and counting..._
- [Access Instance Variables](python/access-instance-variables.md)
- [Create A Dummy DataFrame In Pandas](python/create-a-dummy-dataframe-in-pandas.md)
- [Store And Access Immutable Data In A Tuple](python/store-and-access-immutable-data-in-a-tuple.md)
- [Test A Function With Pytest](python/test-a-function-with-pytest.md)
- [Use pipx To Install End User Apps](python/use-pipx-to-install-end-user-apps.md)

View File

@@ -5,6 +5,8 @@ an array-like object with all of the arguments to the function. Even if not
all of the arguments are referenced in the function signature, they can
still be accessed via the `arguments` object.
> For ES6+ compatibility, the `spread` operator used via [rest parameters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters) is preferred over the `arugments` object when accessing an abritrary number of function arguments.
```javascript
function argTest(one) {
console.log(one);

View File

@@ -0,0 +1,49 @@
# Store And Access Immutable Data In A Tuple
You can store heterogeneous data (of varying types) as a _tuple_ which is a
light-weight immutable data structure.
You can be explicit about the tuple by wrapping the items in parentheses:
```python
>>> book = ('An Immense World', 'Ed Yong', 2022)
```
Though it is also possible to comma-separate the items and forego the
parentheses.
```python
>>> book2 = 'The Shining', 'Stephen King', 1977
>>> book2
('The Shining', 'Stephen King', 1977)
```
Once we have our tuple, we can access any item from it positionally. We can
also use _sequence unpacking_ to assign the values to a series of variables:
```python
>>> book[0]
'An Immense World'
>>> book[1]
'Ed Yong'
>>> book[2]
2022
>>> title, author, publication_year = book
>>> title
'An Immense World'
>>> author
'Ed Yong'
>>> publication_year
2022
```
And, as promised, it is immutable (unlike lists):
```python
>>> book[1] = 'Agatha Christie'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
```
[source](https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences)