hasattr() Has attribute

Last updated: 2026-09-22

hasattr() Has attribute

hasattr(obj, name) reports whether obj has an attribute called name.

CategoryBuilt-in Functions
KindBuilt-in Function
Python Versionall

📝 Syntax

hasattr(object, name)

⚙️ Parameters

object RequiredThe object to check for the attribute (any object).
name RequiredAttribute name as a string, e.g. 'attr'.

Returns:A bool: True if the object has the named attribute, otherwise False.

💥 Raises

▶ Example

Edit the code, press Run, and see the result in the output panel:

Output:

True
False
More case studies coming soon — check back later.

❓ FAQ

QIf hasattr(obj, 'x') returns True, what does that mean?
AIt means obj has an attribute named x (including ones inherited from a parent class), but it does not guarantee the value is non-None, nor that it is callable.
QWhat is the difference between hasattr and getattr?
Ahasattr only checks existence and returns a boolean; getattr fetches the value and raises an AttributeError if it is missing (unless a default is given).
QMust the attribute name be hardcoded?
ANo — the name in hasattr(obj, attr_name) can be a variable, which suits dynamically checking an object's capabilities.
QCan hasattr check a method?
AYes, a method is also an attribute, so hasattr(obj, 'method_name') returns True; combine it with callable() to further confirm it is callable.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏