Python Language OOP: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 25: Line 25:
===<tt>__str__()</tt>===
===<tt>__str__()</tt>===
<code>__str__()</code> is used by <code>print()</code>, <code>str()</code> and the string formatters to produce a string representation of an instance.
<code>__str__()</code> is used by <code>print()</code>, <code>str()</code> and the string formatters to produce a string representation of an instance.
<syntaxhighlight lang='py'>
class MyClass:
  ...
  def __str__(self):
      return self.name + ', ' + self.color
</syntaxhighlight>


==<span id='Static_Method'></span>Static Methods==
==<span id='Static_Method'></span>Static Methods==

Revision as of 20:48, 15 March 2022

External

Internal

Overview

Attributes and methods.

Class

class MyClass:
  def __init__(self):
    pass

The class may be declared with parentheses, but the IDE static checks find those as "redundant":

class MyClass():
  ...

Methods

Special Methods

__str__()

__str__() is used by print(), str() and the string formatters to produce a string representation of an instance.

class MyClass:
  ...
  def __str__(self):
      return self.name + ', ' + self.color

Static Methods

Initialization

Inheritance

Overriding

Polymorphism

.