Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

0x08 Python - More Classes and Objects

The document discusses Python classes and objects including class attributes versus object attributes, static methods, and the special __str__ and __repr__ methods. It provides examples and explanations of each topic.

Uploaded by

ELOTHMANI93
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

0x08 Python - More Classes and Objects

The document discusses Python classes and objects including class attributes versus object attributes, static methods, and the special __str__ and __repr__ methods. It provides examples and explanations of each topic.

Uploaded by

ELOTHMANI93
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

ALX LESSON

0x08 Python -
More Classes
and Objects
python- Programming
TABLE OF CONTENTS

01 02
Overview Learning
topics Objectives

03 04
Quiz hands on lab
questions practice
01
OVERVIEW topics
Topics

What are the special __str__ and __repr__ methods


and how to use them

What is the difference between __str__ and __repr__


python What is a class attribute
Programming What is the difference between a object attribute
Topics and a class attribute

What is a static method


Slides On Telegram

https://t.me/alx_2023

python
Programming
Topics

Slides On Telegram
alx_2023ch
02
Learning Objectives
What is a class attribute

class MyClass:
class_attribute = "I am a class attribute"

def __init__(self, instance_attribute):


self.instance_attribute = instance_attribute

# Creating instances of the class


obj1 = MyClass("Instance 1")
obj2 = MyClass("Instance 2")

# Accessing the class attribute


print(MyClass.class_attribute) # Output: "I am a class attribute"

# Accessing instance attributes


print(obj1.instance_attribute) # Output: "Instance 1"
print(obj2.instance_attribute) # Output: "Instance 2"
What is the difference between a object attribute and a class attribute

class Car:
# Class attribute
fuel_type = "Gasoline"

def __init__(self, make, model):


# Object attributes
self.make = make
self.model = model

# Creating instances of the Car class


car1 = Car("Toyota", "Camry")
car2 = Car("Honda", "Civic")

# Accessing object attributes


print(f"Car 1: {car1.make} {car1.model}, runs on {car1.fuel_type}")
print(f"Car 2: {car2.make} {car2.model}, runs on {car2.fuel_type}")

# Modifying the class attribute


Car.fuel_type = "Electric"

# Accessing class attribute from instances


print(f"Car 1: {car1.make} {car1.model}, runs on {car1.fuel_type}")
print(f"Car 2: {car2.make} {car2.model}, runs on {car2.fuel_type}")
What is a static method

A static method is a method that belongs to a class rather than an instance of


the class. Unlike regular instance methods, static methods do not have access to
the instance's state (attributes) and do not modify it. They are defined within a
class but are not bound to the class or its instances.

Decorator: To define a static method, you use the @staticmethod decorator above
the method definition.

No access to instance-specific data: Static methods don't take the self parameter
as their first argument, which is a convention for instance methods. This means
they cannot access or modify instance attributes.
What is a static method

class MathUtils:
@staticmethod
def add(x, y):
return x + y

@staticmethod
def subtract(x, y):
return x - y

# Calling static methods without creating instances


result1 = MathUtils.add(5, 3)
result2 = MathUtils.subtract(10, 4)

print(f"Addition result: {result1}")


print(f"Subtraction result: {result2}")
What are the special __str__ and __repr__ methods and how to use them and
difference between them

__str__ (User-Friendly Representation):


Purpose: The primary purpose of __str__ is to provide a human-readable, user-
friendly representation of an object.

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def __str__(self):
return f"{self.name}, {self.age} years old"

person = Person("Alice", 30)


print(str(person)) # Output: "Alice, 30 years old"
#or print(person.__str__())
What are the special __str__ and __repr__ methods and how to use them and
difference between them

__repr__ (Developer-Friendly Representation):


Purpose: The primary purpose of __repr__ is to provide an unambiguous,
developer-friendly representation of an object.

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def __repr__(self):
return f"Person('{self.name}', {self.age})"

person = Person("Bob", 25)


print(repr(person)) # Output: "Person('Bob', 25)"
#or print(person.__repr__())
04
Hands on lab Practice
Have a Question
Leave a Comment!
Subscribe
To stay updated with latest
videos

Share
To let the others know more
Thanks

You might also like