Python Language OOP: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 70: Line 70:
<syntaxhighlight lang='py'>
<syntaxhighlight lang='py'>
class MyClass:
class MyClass:
    some_var = 'something'
  some_var = 'something'


    def some_method(self):
  def some_method(self):
        print('some_var value', MyClass.some_var)
    print('some_var value', MyClass.some_var)
        print('some_var value', self.some_var)
    print('some_var value', self.some_var)
</syntaxhighlight>
</syntaxhighlight>



Revision as of 22:27, 15 March 2022

External

Internal

TODO

  • How to call a method from inside the constructor. If I try, the compiler says "Unresolved reference"

Overview

Everything in Python is an object, from numbers to modules. The language offers possibility of declaring custom types, and creating object instances for those types. Unlike modules, there can be multiple instances of the same type. Creation of new types is achieved by using the class syntax. The custom types such declared are no different, intrinsically, from the types already existing in the runtime. The existing types can be extended.

An object contains data, in form of variables called attributes and behavior, in form of functions called methods. The attributes can be accessed inside the class definition using self.<attribute-name>. Outside the class definition, they can be accessed via the variable holding the reference to the class instance: my_instance.<attribute-name>. Unlike in other languages, all attributes and methods are public.

Class

A new type (class) is defined with the class keyword.

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

__init__()

The object instance initialization method, which is used to do anything that's needed to distinguish an object instance from other object instances of the same class. The self argument specifies that it refers to the individual object itself. self is not a reserved word in Python, but it is used conventionally, and it's a good idea to stick with the convention.

class MyClass:
  def __init__(self, name, color):
    self.name = name
    self.color = color

An __init__() method is not required.

__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

Properties, Getters and Setters

In Python, all attributes are public, they can be accessed from outside the class using the variable name that holds a reference to a class' instance.

However, Python has syntax that allows declaring getter and setter methods to access instance state that is not available in form of an actual instance attribute. The instance state in question is known as a property, which is exposed via getter and setter methods.

Static Methods

Static Initialization at Class Level

To perform an initialization once, upon creation of the type in the class' namespace, place the code directly under the code definition: All methods placed into the class are also "executed" - like when defining a function in the global namespace - but they are not calling.

class MyClass:
  # executed only once upon creation of the type
  pattern = re.compile(r'^(\w+):(\w+)-(\w+)$')

  def __init__(self):
    ...
  def my_method(self):
    ...

The static variable can be accessed inside the class (and outside) by prefixing it with the class name, or, within an instance method, by prefixing it with self:

class MyClass:
  some_var = 'something'

  def some_method(self):
    print('some_var value', MyClass.some_var)
    print('some_var value', self.some_var)

Objects

To create an instance of a class, use the class name followed by left parenthesis, followed by constructor arguments, followed by the right parenthesis.

o2 = SomeOtherClass('something')

If only the class name without parentheses is used, the code will work, but the variable will contain a reference to the class, not the instance of the class.

Initialization

Inheritance

Overriding

Polymorphism

.