python / beginner
Snippet
Basic Class Definition
A class acts as a template for creating objects. Methods are functions defined inside a class that perform actions for the object.
snippet.py
1
2
3
4
5
6
class Robot:def action(self):print("Moving...")my_bot = Robot()my_bot.action()
Breakdown
1
class Robot:
Define a new blueprint named Robot.
2
def action(self):
Define a method that belongs to the Robot class.
3
my_bot = Robot()
Create an individual instance of the Robot.