mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-30 22:23:42 +00:00
Added oop_example.py file
This commit is contained in:
parent
0040ad47f9
commit
5443c8ec21
20
other/oop_example.py
Normal file
20
other/oop_example.py
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
# Class Creation
|
||||||
|
class Person:
|
||||||
|
# Initialization function (when you create an object this named initialization)
|
||||||
|
def __init__(self,name,age):
|
||||||
|
# changing objects attributes to arguments that we enter when calling an class to create an object
|
||||||
|
self.name = name
|
||||||
|
self.age = age
|
||||||
|
|
||||||
|
# String function that returns a string that will be returned if you will print class
|
||||||
|
# Without __str__(): <__main__.Person object at 0x000001AC025E7230>
|
||||||
|
# With __str__():
|
||||||
|
def __str__(self):
|
||||||
|
return f'Name: {self.name}. Age: {self.age}'
|
||||||
|
|
||||||
|
# Creating an object using class
|
||||||
|
p = Person("John",21)
|
||||||
|
print(p)
|
||||||
|
|
||||||
|
# Output:
|
||||||
|
# Name: John. Age: 21
|
Loading…
Reference in New Issue
Block a user