-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClasses.py
99 lines (66 loc) · 2.98 KB
/
Classes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#* Classes - Python
# In Python, a class is a template for creating objects. Objects are instances of a
# class, and each object can have associated attributes and methods. Here is a basic description of
# how to work with classes in Python:
#? Definition of a Class:
# To define a class in Python, use the `class` keyword, followed by the class name and
# colon `:`. The body of the class contains attributes and methods.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print(f"Hello, I am {self.name} and I am {self.age} years old.")
# In this example, `Person` is the name of the class. The `__init__` function is a special constructor
# which is called when a new object of the class is created. `self` is a reference to the current object
# and is used to access the object's attributes.
#? Object Creation:
# Once you have defined a class, you can create objects (instances of the class) using the syntax
# of function call.
john = Person("John", 25)
ana = Person("Ana", 30)
john.greet() # Print: Hello, I'm Juan and I'm 25 years old.
ana.greet() # Print: Hello, I am Ana and I am 30 years old.
#? Attributes and Methods:
#* - **Attributes:**
# They are variables that store information about the object.
#* - **Methods:**
# They are functions defined within a class and operate on the attributes of the object.
#? The `__init__` Method:
# It is a special method called a constructor. It runs automatically when a new object is created and
# is used to initialize the object's attributes.
#? Access to Attributes and Methods:
# You can access the attributes and methods of an object using dot notation.
john_name = john.name
john.greet()
#? Encapsulation:
# In Python, there is no strict encapsulation like in some other programming languages, but you can
# conventionally indicate that an attribute or method is "private" by adding an underscore before the
# name.
class Car:
def __init__(self, make, model):
self._brand = make
self._model = model
def get_information(self):
return f"{self._brand} {self._model}"
my_car = Car("Toyota", "Camry")
print(my_car._brand) # "Private" access
#? Inheritance:
# Allows a class (subclass) to inherit attributes and methods from another class (superclass).
class Animal:
def make_sound(self):
print("Makes some sound")
class Dog(Animal):
def make_sound(self):
print("Woof!")
class Cat(Animal):
def make_sound(self):
print("Meow!")
dog = Dog()
cat = Cat()
dog.make_sound() # Print: Woof!
cat.make_sound() # Print: Meow!
# These are some basics on how to work with classes in Python. Classes are
# fundamentals in Object Oriented Programming - Poo and allow you to organize and structure
# your code more efficiently and reusable. Practice and experiment to strengthen your
# skills in using classes in Python!.