Class-Object-Constructor-Object-Oriented-Programming-OOP-in-Python
Class-Object-Constructor-Object-Oriented-Programming-OOP-in-Python
Programming (OOP)
Class, Object, Constructor,
Turtle in Python
Attributes Methods
Attributes define the Methods define the actions or
characteristics or properties of behaviors that an object can
an object. They store data perform. They are functions
associated with an object. associated with a class.
3 Methods 4 Constructor
Methods are functions The __init__ method is the
defined within a class to constructor, used for
encapsulate behavior. initializing objects.
Defining a Class in Python
A class is defined using the 'class' keyword followed by the class name and
a colon (:).
class Dog:
def __init__(self, name, breed):
self.name = name
self.breed = breed
def bark(self):
print("Woof!")
Initializing Objects with Constructors
The constructor method is usually named __init__ and takes the 'self'
parameter, which represents the object itself, along with other parameters
for initializing attributes.
class Dog:
def __init__(self, name, breed):
self.name = name
self.breed = breed
def bark(self):
print("Woof!")
Creating Objects from a
Class
Objects are created from a class using the class name followed by
parentheses.
Initialization Consistency
It initializes the attributes of an object with default or user- It ensures that objects are created in a consistent state and
specified values. ready to use.
Example of OOP Implementations
in Python
Here's a simple example demonstrating the implementation of OOP principles in Python.
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def start(self):
print(f"The {self.make} {self.model} is starting.")
def stop(self):
print(f"The {self.make} {self.model} has stopped.")
Other Methods
Provides functions for controlling the turtle's movement, drawing, and
other actions.
Turtle Object Instantiation
Class Definition
The blueprint for creating turtle objects.
Object Creation
Instantiating a turtle object from the class.
Object Usage
Accessing and manipulating the turtle's attributes and
methods to create graphics.
Turtle Object Attributes and
Methods
Attribute Description
3 Customization
Adjusting turtle attributes like color, pen size, and speed to create
desired effects.
4 Finalization
Ending the program and displaying the generated image.
Practical Applications of Turtle Graphics
A fun and engaging way to teach Creating dynamic and interactive Building simple games where turtles
programming concepts, especially artwork that responds to user input. interact with each other and the
geometry and design. environment.
Implementing Turtle
Graphics in Python
class MyTurtle:
def __init__(self, color, shape):
# Membuat objek turtle
self.t = turtle.Turtle() # Object dari class Turtle
self.t.color(color) # Mengatur warna turtle
self.t.shape(shape) # Mengatur bentuk turtle
def selesai(self):
# Method untuk menyelesaikan gambar
turtle.done()
# Menyelesaikan gambar
turtle1.selesai()
Analyzing the Code Structure and Approach