functions are reuasable block of code;you can juts reuse a block of code by calling the fucntion name anywhere in the code.
sample code:
def myFunct():
fname = input("What is your first name? ")
lname = input("What is your last name? ")
return f"Your first and last names are: {fname}, {lname} respectively"
This function will:
Ask the user for their first and last name.
Return a formatted string with both names.
You can call this function as many times as you want without rewriting the input logic each time!
Moving straight to the point...
What does "dunder" mean?
"Dunder" is short for double underscore (like init, __str, etc.).
These are special methods in Python that Python calls automatically when certain things happen, like creating a new object, printing an object, or adding two objects together.And
Dunder Methods: What do they do?
Dunder methods are like automatic rules for Python. They are built-in functions that are triggered in specific situations, and you don't need to call them yourself.
Examples:
- init : this initializes any new object for a class
Sample Code for Dunder Methods
class Player:
# Define a new class named Player
def __init__(self, name):
# This function is automatically called when creating a new Player object
# self refers to the current object you're creating
self.name = name # Whatever name you give me, I’ll remember it as self.name
def __str__(self):
# This function is automatically called when you try to print the Player object
return f"Player: {self.name}" # Return the actual player's name when printed
# Creating a Player object
name = input("What is your name? ") # Ask for the player's name
player1 = Player(name) # Create an instance of the Player class with the input name
# Print the player object
print(player1) # This will automatically call __str__ and print "Player: [Name]"
TLDR?
The syntax is simple :
- 1. Class Definition:
A class is a blueprint or template that encapsulates properties and behaviors for objects.
A class contains attributes (data) and methods (functions).
class Player:
- 2. Constructor Method (init):
The constructor method init is a special dunder method used to initialize an object when it's created.
It contains parameters that are passed when you create an instance of the class.
The self keyword refers to the current instance (object) of the class.
def __init__(self, name, age):
self.name = name # Save the player's name
self.age = age # Save the player's score
- 3. Operation Method (e.g., str, add, etc.): This is where you define custom behavior for common operations.
str: Defines how the object will be represented as a string (when you print it).
add: Defines what happens when you use the + operator with objects of that class.
def __str__(self):
return f"Player: {self.name}, Age: {self.age}"
def __add__(self, other):
return self.score + other.score # Add scores of two Player objects
- 4. Key Parameters:
self: A reference to the current object. It’s used inside methods to access the object's attributes (e.g., self.name, self.age).
Other parameters: These are the values you pass into the class constructor or operation methods to define the object's state or behavior.
class Person:
def __init__(self, name, age):
# This is the constructor method to initialize the object
self.name = name # Store the name in the object
self.age = age # Store the age in the object
def __str__(self):
# This is the __str__ method to return a string representation of the object
return f"{self.name} is {self.age} years old."
# Example Usage
person1 = Person("Alice", 25) # Create a Person object with name 'Alice' and age 25
print(person1) # It will print: Alice is 25 years old.
Top comments (0)