|
| 1 | +# Class Creation |
| 2 | +class Person: |
| 3 | + # Initialization function (when you create an object this named initialization) |
| 4 | + def __init__(self,name,age): |
| 5 | + # changing objects attributes to arguments that we enter when calling an class to create an object |
| 6 | + self.name = name |
| 7 | + self.age = age |
| 8 | + |
| 9 | + # String function that returns a string that will be returned if you will print class |
| 10 | + # Without __str__(): <__main__.Person object at 0x000001AC025E7230> |
| 11 | + # With __str__(): |
| 12 | + def __str__(self): |
| 13 | + return f'Name: {self.name}. Age: {self.age}' |
| 14 | + |
| 15 | +# Creating an object using class |
| 16 | +p = Person("John",21) # you can change the values and create some other methods in class |
| 17 | +print(p) |
| 18 | + |
| 19 | +# Output: |
| 20 | +# Name: John. Age: 21 |
0 commit comments