{"componentChunkName":"component---node-modules-smooth-doc-src-templates-doc-js","path":"/docs/classmethod/","result":{"data":{"mdx":{"fields":{"pageType":"doc","title":"Class 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\": \"Class Methods\",\n  \"section\": \"Quick Docs\",\n  \"order\": 4\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, \"Although most methods are tied to an \", mdx(\"em\", {\n    parentName: \"p\"\n  }, \"object instance\"), \", it can sometimes be useful to create a method that does something with \", mdx(\"em\", {\n    parentName: \"p\"\n  }, \"the class itself\"), \". To achieve this in Python, you can use the \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"@classmethod\"), \" decorator. This is often used to provide alternative constructors for a class.\"), mdx(\"p\", null, \"For example, you may be writing a class that takes some magic token (like an API key) as a constructor argument, but you sometimes read this token from a configuration file. You could make use of a \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"@classmethod\"), \" to create an alternate constructor for when you want to read from the configuration file.\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-py\"\n  }, \"class Bot:\\n    def __init__(self, token: str):\\n        self._token = token  \\n\\n    @classmethod\\n    def from_config(cls, config: dict) -> Bot:\\n        token = config['token']\\n        return cls(token)\\n\\n# now we can create the bot instance like this\\nalternative_bot = Bot.from_config(default_config)\\n\\n# but this still works, too\\nregular_bot = Bot(\\\"tokenstring\\\")\\n\")), mdx(\"p\", null, \"This is just one of the many use cases of \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"@classmethod\"), \". A more in-depth explanation can be found \", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"https://stackoverflow.com/questions/12179271/meaning-of-classmethod-and-staticmethod-for-beginner#12179752\"\n  }, \"here\"), \".\"));\n}\n;\nMDXContent.isMDXComponent = true;","tableOfContents":{}}},"pageContext":{"id":"366019a8-3374-525d-8ab4-70e11e282075"}},"staticQueryHashes":["1022386572","1122327541","1339625561","2140385554","3442269102","3597609067"]}