shashikantsingh
Posted on November 7, 2020
To display all the attributes, let's create a class name Laliga,
class Laliga:
def __init__(self,team_name,star_palyer,coach,no_of_titles):
self.team_name = team_name
self.star_palyer = star_palyer
self.coach = coach
self.no_of_titles = no_of_titles
The init_ method will accept the instance of that class as a self and we will have to pass the other 4 arguments.
To understand self, first, create the instance of our class, we call it "team1"
team1 = Laliga("Real madrid","Eden hazard","Zinedine zidane",34)
so when we create method within class,it automatically receives instance of that class as the first argument which
means calling self.team_name has the same meaning as calling team1.team_name.
so now we have understood about self, go back to to printing all the attributes of team1 object to see what its attributes contain, and for that, we have to use dict method
print(team1.__dict__)
This will print the list of all attributes and values contained in it
python class_self.py
{'team_name': 'Real madrid', 'coach': 'Zinedine zidane', 'star_palyer': 'Eden hazard', 'no_of_titles': 34}
now you can see the attributes of team1 object .:)
Posted on November 7, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 3, 2024
November 13, 2024
November 12, 2024