{"componentChunkName":"component---node-modules-smooth-doc-src-templates-doc-js","path":"/docs/dundermethods/","result":{"data":{"mdx":{"fields":{"pageType":"doc","title":"Dunder methods","editLink":""},"body":"var _excluded = [\"components\"];\nfunction _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n/* @jsxRuntime classic */\n/* @jsx mdx */\n\nvar _frontmatter = {\n  \"title\": \"Dunder methods\",\n  \"section\": \"Quick Docs\",\n  \"order\": 12\n};\nvar layoutProps = {\n  _frontmatter: _frontmatter\n};\nvar MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  var components = _ref.components,\n    props = _objectWithoutProperties(_ref, _excluded);\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"p\", null, \"Double-underscore methods, or \\\"dunder\\\" methods, are special methods defined in a class that are invoked implicitly. Like the name suggests, they are prefixed and suffixed with dunders. You've probably already seen some, such as the \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"__init__\"), \" dunder method, also known as the \\\"constructor\\\" of a class, which is implicitly invoked when you instantiate an instance of a class.\"), mdx(\"p\", null, \"When you create a new class, there will be default dunder methods inherited from the \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"object\"), \" class. However, we can override them by redefining these methods within the new class. For example, the default \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"__init__\"), \" method from \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"object\"), \" doesn't take any arguments, so we almost always override that to fit our needs.\"), mdx(\"p\", null, \"Other common dunder methods to override are \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"__str__\"), \" and \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"__repr__\"), \". \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"__repr__\"), \" is the developer-friendly string representation of an object - usually the syntax to recreate it - and is implicitly called on arguments passed into the \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"repr\"), \" function. \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"__str__\"), \" is the user-friendly string representation of an object, and is called by the \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"str\"), \" function. Note here that, if not overriden, the default \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"__str__\"), \" invokes \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"__repr__\"), \" as a fallback.\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-py\"\n  }, \"class Foo:\\n    def __init__(self, value):  # constructor\\n        self.value = value\\n    def __str__(self):\\n        return f\\\"This is a Foo object, with a value of {self.value}!\\\"  # string representation\\n    def __repr__(self):\\n        return f\\\"Foo({self.value!r})\\\"  # way to recreate this object\\n\\n\\nbar = Foo(5)\\n\\n# print also implicitly calls __str__\\nprint(bar)  # Output: This is a Foo object, with a value of 5!\\n\\n# dev-friendly representation\\nprint(repr(bar))  # Output: Foo(5)\\n\")), mdx(\"p\", null, \"Another example: did you know that when you use the \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"<left operand> + <right operand>\"), \" syntax, you're implicitly calling \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"<left operand>.__add__(<right operand>)\"), \"? The same applies to other operators, and you can look at the \", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"https://docs.python.org/3/library/operator.html\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"a\"\n  }, \"operator\"), \" built-in module documentation\"), \" for more information!\"));\n}\n;\nMDXContent.isMDXComponent = true;","tableOfContents":{}}},"pageContext":{"id":"e9d91f8a-0a92-51d1-bac0-56ab548d4fc0"}},"staticQueryHashes":["1022386572","1122327541","1339625561","2140385554","3442269102","3597609067"]}