Welcome to the Python OOP - Static and Dunder Functions repository! This project explores the concepts of static methods, dunder methods, and class members in Python.
This repository provides a comprehensive guide to understanding static and dunder functions in Python. By working through the provided examples, you will learn how to define and use class members, instance members, static methods, and dunder methods effectively.
To get started, check out the Releases section for the latest version. Download and execute the files to explore the functionalities.
Class members are attributes and methods that belong to the class itself rather than to any specific instance. They are defined within the class and can be accessed using the class name. Class members are useful for defining properties that should be shared across all instances.
Instance members are attributes and methods that belong to a specific instance of a class. Each instance can have different values for its instance members. You define instance members using the self
keyword in the class constructor.
Static methods are defined using the @staticmethod
decorator. They do not require access to the instance (self
) or class (cls
) and can be called on the class itself. Static methods are useful for utility functions that do not need to modify class or instance state.
class Example:
@staticmethod
def static_method():
return "This is a static method."
Class methods are defined using the @classmethod
decorator. They take a reference to the class (cls
) as their first parameter. Class methods can modify class state that applies across all instances.
class Example:
class_variable = 0
@classmethod
def class_method(cls):
cls.class_variable += 1
return cls.class_variable
Dunder methods, or magic methods, allow you to define how objects of your class behave with built-in Python functions. Common dunder methods include __init__
, __str__
, and __repr__
. These methods start and end with double underscores.
class Example:
def __init__(self, value):
self.value = value
def __str__(self):
return f"Example with value: {self.value}"
Getters and setters allow controlled access to instance attributes. You can use the @property
decorator to define a getter and the @<property_name>.setter
decorator to define a setter.
class Example:
def __init__(self):
self._value = 0
@property
def value(self):
return self._value
@value.setter
def value(self, new_value):
if new_value >= 0:
self._value = new_value
To install this project, clone the repository and navigate to the project directory:
git clone https://github.com/finlckd2/python-OOP-Lec4-21-MAY-25.git
cd python-OOP-Lec4-21-MAY-25
You can also download the ZIP file from the Releases section and extract it.
After installation, you can run the examples provided in the examples
directory. Each example illustrates a different concept related to static and dunder functions.
To run an example, use the following command:
python examples/example_file.py
Here are some examples demonstrating the key concepts:
class MathUtils:
@staticmethod
def add(a, b):
return a + b
result = MathUtils.add(5, 3)
print(result) # Output: 8
class Counter:
count = 0
@classmethod
def increment(cls):
cls.count += 1
Counter.increment()
print(Counter.count) # Output: 1
class Person:
def __init__(self, name):
self.name = name
def __str__(self):
return f"Person: {self.name}"
person = Person("Alice")
print(person) # Output: Person: Alice
class Age:
def __init__(self):
self._age = 0
@property
def age(self):
return self._age
@age.setter
def age(self, value):
if value >= 0:
self._age = value
age_instance = Age()
age_instance.age = 25
print(age_instance.age) # Output: 25
Contributions are welcome! If you would like to improve this project, please fork the repository and submit a pull request.
- Fork the repository.
- Create a new branch for your feature or fix.
- Make your changes and commit them.
- Push to your branch and create a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
Feel free to explore the repository and learn more about Python's object-oriented programming features. For the latest updates, visit the Releases section.