Lecture_3#_Class_definition
Lecture_3#_Class_definition
Using
Python
Teemu Matilainen
teemu.matilainen@sav
onia.fi
Lecture 3#
This line sets the baby_birds attribute of the Bird instance. If a value
for baby_birds is provided during the object creation (Bird(...,
baby_birds=some_value)), it will be used. Otherwise, it defaults to an
empty list ([]). This is a way to handle the case where baby_birds might
not be provided, ensuring that self.baby_birds is always a list.
How to print an object?
• Static method is a
method that belongs to a
class rather than an
instance of the class. It is
defined using the
@staticmethod
decorator and does not
have access to the
instance or its attributes.
Static methods are
similar to class methods,
but they do not receive
the class itself as the first
parameter.
Key characteristics of static methods:
• How to create a
dynamic method
bound to an instance
of a class by using the
types module,
specifically the
types.MethodType
class. This allows you
to dynamically add a
method to an instance
of a class at runtime.
Data encapsulation
• Attributes (Data Members): These are the variables that store the state of an
object. They are typically declared as private or protected within the class to
restrict direct access from outside.
• Getters and Setters: These are methods that are used to access (get) or
modify (set) the values of private attributes. By using getters and setters, you
can control how the data is accessed and modified, enforcing validation or
business rules if needed.
In object-oriented programming, access modifiers determine the visibility of
class members (attributes and methods) from outside the class. There are
typically three main access modifiers: public, private, and protected.
Here's a brief overview of each:
Public:
Members declared as public are accessible from anywhere, both within the
class and from external code. There are no restrictions on accessing public
members.
Private:
Members declared as private are only accessible from within the same
class. External code cannot directly access or modify private members.
Protected:
Members declared as protected are accessible within the same class and its
subclasses (derived classes or child classes). External code, which is not a
subclass, typically cannot access or modify protected members.
In Python, the access modifiers
public, private, and protected
are conventions rather than
strict rules, as Python is a
language that emphasizes
readability and simplicity. The
convention is to use a single
underscore _ to indicate a
protected member, and a
double underscore __ to
indicate a private member.
It's important to note that Python does not enforce strict encapsulation or access control like
some other languages. The use of a single underscore (_) is a convention to indicate that a
variable or method should be treated as protected, and developers are expected to follow this
convention. However, external code can still access protected members if needed. Private
members (those with double underscores (__) are name-mangled to make them less accessible
from outside the class, but they are not entirely inaccessible.
Here's a
modified
version of the
Car class
example with
private
attributes:
Here's a
modified
version of the
Car class
example with
protected
attributes:
Visibility of methods
Public Methods:
Methods without any special naming convention are considered public and can be
accessed from anywhere.
Example: def public_method(self):
Protected Methods:
Private Methods:
• __init__(self)
• Initializes the stopwatch with an initial state.
• start(self)
• Starts the stopwatch. If the stopwatch is already running, do nothing.
• stop(self)
• Stops the stopwatch. If the stopwatch is not running, do nothing.
Output example:
• reset(self)
• Resets the stopwatch to its initial state.
• elapsed_time(self)
• Returns the elapsed time in seconds. If the stopwatch is running, it should
return the time elapsed up to the current moment. If the stopwatch is
stopped or reset, it should return the total elapsed time.