# 命名元组代替class

{% hint style="info" %}
使用命名元组 **namedtuple** 可以代替简单的class定义
{% endhint %}

```python
>>> from collections import namedtuple
>>> Car = namedtuple('Car', 'color mileage')

>>> my_car = Car('red', 3812.4)
>>> my_car.color
'red'
>>> my_car.mileage
3812.4

# 打印命名元组信息
>>> my_car
Car(color='red' , mileage=3812.4)

# 命名元组同元组（tuple）一样，无法修改（immutable）
>>> my_car.color = 'blue'
AttributeError: "can't set attribute"
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://m0uk4.gitbook.io/notebooks/mouka/python/python-tricks/ming-ming-yuan-zu-dai-ti-class.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
