Python Language OOP: Difference between revisions
Jump to navigation
Jump to search
(→Class) |
|||
Line 24: | Line 24: | ||
... | ... | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==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. | |||
<syntaxhighlight lang='py'> | |||
class MyClass: | |||
pattern = re.compile(r'^(\w+):(\w+)-(\w+)$') | |||
def __init__(self): | |||
... | |||
def my_method(self): | |||
... | |||
</syntaxhighlight> | |||
==<span id='Method'></span>Methods== | ==<span id='Method'></span>Methods== |
Revision as of 21:31, 15 March 2022
External
Internal
TODO
- How to call a method from inside the constructor. If I try, the compiler says "Unresolved reference"
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():
...
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:
pattern = re.compile(r'^(\w+):(\w+)-(\w+)$')
def __init__(self):
...
def my_method(self):
...
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
.