0% found this document useful (0 votes)
12 views18 pages

Python 4 Object

The document provides an overview of object-oriented programming concepts, focusing on the definition, attributes, and methods of objects in Python. It includes examples of class definitions, object instantiation, and the use of special methods like __init__ and __str__. Additionally, it illustrates the comparison between methods and functions, along with practical examples involving a Ball and HotDog class.

Uploaded by

sihyun6876
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views18 pages

Python 4 Object

The document provides an overview of object-oriented programming concepts, focusing on the definition, attributes, and methods of objects in Python. It includes examples of class definitions, object instantiation, and the use of special methods like __init__ and __str__. Additionally, it illustrates the comparison between methods and functions, along with practical examples involving a Ball and HotDog class.

Uploaded by

sihyun6876
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Object (객체)

김은솔
이상화
Object (객체)
Object: Independent unit of data and functions
Object examples: string, list, dictionary, file
Attribute (속성, 변수)
Variables to describe the object
Method (함수)
Function to do something with the object
Object (객체)
Object definition & instance
class object_name : # design about the object
Instance_object = object_name( arguments ) # realization of the design
Object usage: dot notation
instance_object.attribute
instance_object.method( arguments)
Object Definition
Object design and object instance declaration

class Ball: # Object class declaration


size = 10 # Attributes of the Ball class
………….

def method (self): # Method of the Ball object


…………
return result

myBall = Ball ( ) # Declaration of Ball class instance


[Link] = 25 # Attribute assignment of myBall object
Object: Definition and Instance
Example 1
class Ball: # class (object) definition
def bounce(self): # method definition
if [Link] == "down": # self: indicating the instance object (conventional)
[Link] = "up“ (instance reference)

1 myBall = Ball( ) # Create an object instance without arguments


[Link] = "down" # Set some attributes
[Link] = "red"
[Link] = "small"

direction = “Right” # declare a usual variable


print ( "My ball is ", [Link], [Link] ) # Display object attributes
print ( "My ball's direction is", [Link] )
print ( "Now I'm going to bounce the ball“)

[Link]( ) # bounce the ball


print ( "Now the ball's direction is", [Link] )
print ( “Another direction is ", direction ) # Not attribute of ball object
Object: Initialization (초기화)
Example 2 ( Python special method: __init__ ( ) )
class Ball: # class (object) definition
def __init__(self, color, size, direction): # attribute initialization using __init__ () method
[Link] = color
[Link] = size
[Link] = direction

def bounce(self): # method definition


if [Link] == "down":
[Link] = "up"

myBall = Ball("red“, "small“, "down“ ) # Create an object instance with arguments (initialization)

print ( "My ball is ", [Link], [Link] ) # Display object attributes


print ( "My ball's direction is", [Link] )
print ( "Now I'm going to bounce the ball“)

[Link]() # bounce the ball


print ( "Now the ball's direction is", [Link] )
Object: Printing (인쇄)
Python special method : __str__ ()
Formatting the printed output about objects
print (myBall) => <__main__.Ball instance at 0x00BBAAAA>
__main__: function where the instance is defined
Ball: class name
0x00BBAAAA: memory address where the instance is stored
Object: Printing (인쇄)
Example 3
class Ball:
………….
………….

#formatting printed output about object

def __str__(self):
msg = "Hi, I'm a " + [Link] + “ and " + [Link] + " ball."
return msg

myBall = Ball ("red“, "small“, "down“ )

print (myBall) #printing object with returned msg


Comparison of Method with Function
class Ball:
def __init__(self, color, size, direction): ball1 = Ball("red“, "small“, "down“ )
[Link] = color print ( "My ball is ", [Link], [Link] )
[Link] = size print ( "My ball's direction is", [Link] )
[Link] = direction [Link]()
print ( "Now the ball1's direction is", [Link] )
def __str__(self):
msg = "Hi, I'm a " + [Link] ball2 = Ball(“Blue”, “Large”, “left”)
mag += “ and " + [Link] + " ball." [Link]()
return msg direction = bounce ([Link])
print ("Now the ball2's direction is", [Link])
def bounce(self): print (direction)
if [Link] == "down":
[Link] = "up“ [Link] ( )
elif [Link] == “left” : print ("Now the ball2's direction is", [Link])
[Link] = “right”
ball3 = Ball(“orange”, “medium”, “up”)
# another function is defined out of object [Link] = “Korea”
def bounce(direction): print ( “Ball3’ maker is “, [Link])
if direction == “left": print ( “Ball1’s maker is “, [Link])
return “right"
Object: Example 4 (1)
1/2 2/2
Object: Example 4 (2-1)
1/2
Object: Example 4 (2-2)

1/2
Object: Example 5
Object: Example 6 (1/3)
# Hotdog class with cook(), add_condiments(), __init__(), __str__()

class HotDog:
def __init__(self):
self.cooked_level = 0
self.cooked_string = "Raw"
[Link] = [ ] # empty list definition to append something

# Define __str__() method, which displays hotdog, including condiments


def __str__(self):
msg = "hot dog"
if len([Link]) > 0: # len () : length of list
msg = msg + " with "
for i in [Link]:
msg = msg + i + ", "
msg = [Link](", ") # strip(‘,’) : delete the last “,” in the msg string
msg = self.cooked_string + " " + msg + "."
return msg
Object: Example 6 (2/3)
def cook(self, time): #define a method to cook hotdog
self.cooked_level=self.cooked_level + time

if self.cooked_level > 8: #set the string of cooked level


self.cooked_string = "Charcoal"
elif self.cooked_level > 5:
self.cooked_string = "Well-done"
elif self.cooked_level > 3:
self.cooked_string = "Medium"
else:
self.cooked_string = "Raw"

def addCondiment(self, condiment): #define a method to add condiments


[Link](condiment)
Object: Example 6 (3/3)
myDog = HotDog() # create an instance

# test the methods Execution result


print (myDog)
print ("Cooking hot dog for 4 minutes..." ) Raw hot dog.
[Link](4) Cooking hot dog for 4 minutes...
print (myDog) Medium hot dog.

print ("Cooking hot dog for 3 more minutes..." ) Cooking hot dog for 3 more minutes...
[Link](3) Well-done hot dog.
print (myDog)
What happens if I cook it for 10 more minutes?
print "What happens if I cook it for 10 more minutes?" ) Charcoal hot dog.
[Link](10)
print (myDog) Now, I'm going to add some stuff on my hot dog
Charcoal hot dog with ketchup, mustard.
print ( "Now, I'm going to add some stuff on my hot dog" )
[Link]("ketchup")
[Link]("mustard")
print (myDog)
Object: Example 7
Define a list with object instances as elements
Example
class Score
Attributes: math, python, korean
Method: Calculate_mean ( )
__str__ ( ) define
Print scores of three subjects
Print the average score
A list variable Students = [] declare
Append score classes as elements
Object: Implementation of Example 7

You might also like