{"id":135,"date":"2024-01-25T13:28:58","date_gmt":"2024-01-25T13:28:58","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=135"},"modified":"2024-01-25T13:28:59","modified_gmt":"2024-01-25T13:28:59","slug":"python-class","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/25\/python-class\/","title":{"rendered":"Python Class"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about Python classes and objects and how to define a new class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Objects<\/h2>\n\n\n\n<p>An object is a container that contains&nbsp;<strong>data<\/strong>&nbsp;and&nbsp;<strong>functionality<\/strong>.<\/p>\n\n\n\n<p>The&nbsp;<strong>data<\/strong>&nbsp;represents the object at a particular moment in time. Therefore, the data of an object is called the&nbsp;<strong>state<\/strong>. Python uses&nbsp;<strong>attributes<\/strong>&nbsp;to model the state of an object.<\/p>\n\n\n\n<p>The functionality represents the&nbsp;<strong>behaviors<\/strong>&nbsp;of an object. Python uses&nbsp;<a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-functions\/\">functions<\/a>&nbsp;to model the behaviors. When a function is associated with an object, it becomes a&nbsp;<strong>method<\/strong>&nbsp;of the object.<\/p>\n\n\n\n<p>In other words, an object is a container that contains the&nbsp;<strong>state<\/strong>&nbsp;and&nbsp;<strong>methods<\/strong>.<\/p>\n\n\n\n<p>Before creating objects, you define a class first. And from the class, you can create one or more objects. The objects of a class are also called&nbsp;<strong>instances<\/strong>&nbsp;of a class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Define a class<\/h2>\n\n\n\n<p>To define a class in Python, you use the&nbsp;<code>class<\/code>&nbsp;keyword followed by the class name and a colon. The following example defines a&nbsp;<code>Person<\/code>&nbsp;class:<code>class Person: pass<\/code><small>Code language: Python (python)<\/small><\/p>\n\n\n\n<p>By convention, you use capitalized names for classes in Python. If the class name contains multiple words, you use the&nbsp;<code>CamelCase<\/code>&nbsp;format, for example&nbsp;<code>SalesEmployee<\/code>.<\/p>\n\n\n\n<p>Since the&nbsp;<code>Person<\/code>&nbsp;class is incomplete; you need to use the&nbsp;<code><a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-pass\/\">pass<\/a><\/code>&nbsp;statement to indicate that you\u2019ll add more code to it later.<\/p>\n\n\n\n<p>To create an instance of a class, you use the class name with parentheses like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>person = Person()<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>When printing out the\u00a0<code>person<\/code>\u00a0object, you\u2019ll see its name and memory address:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Person: pass print(person)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&lt;__main__.Person object at 0x000001C46D1C47F0> <\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>To get an identity of an object, you use the id() function. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>print(id(person))<\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>1943155787760<\/code><\/code><\/pre>\n\n\n\n<p>The id of an object is unique. In CPython, the id() returns the memory address of an object. The hex() function converts the integer returned by the id() function to a lowercase hexadecimal string prefixed with 0x:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>print(hex(id(person)))<\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>0x1c46d1c47f0<\/code><\/code><\/pre>\n\n\n\n<p>The person object is an instance of the\u00a0<code>Person<\/code>\u00a0class. The\u00a0<code>isinstance()<\/code>\u00a0function returns\u00a0<code>True<\/code>\u00a0if an object is an instance of a class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>print(isinstance(person, Person)) <em># True<\/em> <\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">A class is also an object in Python<\/h2>\n\n\n\n<p>Everything in Python is an object, including classes.<\/p>\n\n\n\n<p>When you define the\u00a0<code>Person<\/code>\u00a0class, Python creates an object with the name\u00a0<code>Person<\/code>. The\u00a0<code>Person<\/code>\u00a0object has attributes. For example, you can find its name using the\u00a0<code>__name__<\/code>\u00a0attribute:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>print(Person.__name__)<\/code><small>Code language: CSS (css)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Person<\/code><\/code><\/pre>\n\n\n\n<p>The\u00a0<code>Person<\/code>\u00a0object has the type of\u00a0<code>type<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>print(type(Person))<\/code><small>Code language: PHP (php)<\/small><\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>&lt;class 'type'><\/code><small>Code language: HTML, XML (xml)<\/small><\/code><\/pre>\n\n\n\n<p>The\u00a0<code>Person<\/code>\u00a0class also has a behavior. For example, it can create a new instance:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>person = Person()<\/code><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about Python classes and objects and how to define a new class. Objects An object is a container that contains&nbsp;data&nbsp;and&nbsp;functionality. The&nbsp;data&nbsp;represents the object at a particular moment in time. Therefore, the data of an object is called the&nbsp;state. Python uses&nbsp;attributes&nbsp;to model the state of an object. The functionality represents [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-135","post","type-post","status-publish","format-standard","hentry","category-1-classes-and-objects"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=135"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/135\/revisions"}],"predecessor-version":[{"id":136,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/135\/revisions\/136"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}