((use this or not??))
Standard method, __str__()
Let's introduce another commonly used method in about every class, __str__()
. look at it's name: the same naming convention as the initialization method __init__
, there are two underscores before and after the name. It is common that Python uses this naming technique for special methods.
method __str__( )
is responsible for returning a string representation as defined by the class creator. It's for the users to be able to print the object.
Pet
Class
Let's see __str__()
in a Pet
class example:
# Pet
class Pet:
def __init__(self, name, type, age):
self.name = name
self.type = type
self.age = age
def __str__ (self):
return " %s is a %s, aged %d." % (self.name, self.type, self.age)
The above example, the Pet
class begins with the standard initialization method __init__
function, when we create a Pet
object, it’s three instance variables: name, type
and age
will be initialized.
The next method is __str__
, it is required that the __str__
method create and return a string. If we call print()
on the objects, Python automatically use this string.
Let's create a Pet
object and see how __str__
works:
h = Pet('Sniffy', 'hamster', 2)
print(h.name) # Sniffy
print(h.age) # 2
print(h) # Sniffy is a hamster, aged 2.
Look at the last print()
line, when we call print()
on the object h
, the string message specified in the class method __str__
is printed out in the console.
Note:
It is required that the
__str__
method create and return a string.When we use method
__str__()
, we don't call it's name, we callprint()
function with class object's name as the input. This is the special significance of this method.
Another class example, Dress
class Dress:
def __init__(self, color, size):
self.color = color
self.size = size
def __str__(self):
return "%s dress, size %d" % (self.color, self.size)
# class instance objects
d1 = Dress('white', 0)
d2 = Dress('blue', 5)
print(d1) # white dress, size 0
print(d2) # blue dress, size 5
print(d1.size) # 0
print(d2.color) # blue
# change instance variable values
d1.size = 8
d2.color = 'orange'
print(d1) # white dress, size 8
print(d2) # orange dress, size 5
Each Dress
object has it's own color
and size
, when you call print(object)
, the string message specified in the special method __str__()
is printed out with each object's unique data.
====old below
(( ???may remove setter getter
setter, getter
Setter and getter methods are often used in most classes. The setter methods are for changing the data. The getter methods do not change the values of attributes, they just return the values. By doing these, the attributes of a class are made private to hide and protect them from other code. This is called encapsulation.
We will make a Pet
class, and add setter, getter methods to it:
Ex. 11.4-1 Pet.py
# Pet.py
class Pet:
def __init__(self, name=’Buzz’, age=1):
self.name = name
self.age = age
def setName(self, name):
self.name = name
def setAge(self, age):
self.age = age
def getName(self, name):
return self.name
def getAge(self, age):
return self.age
def __str__ (self):
return “ %s is aged %d.” % (self.name, self.age)
The above example, the Pet
class begins with the standard constructor __init__
function, when we create a Pet
object, it’s two instance variables: name
and age
will be initialized. If the user fails to provide these values, they default to ‘Buzz’
and 1
.
The setName( )
and setAge( )
are setters, getName( )
and getAge( )
are getters, they are the two types of accessors, provide write-only or read-only access to the underlying value.
Use setter and getter Ex. 11.4-1 continued, Person.py
# use Pet
p1 = Pet()
print( p1.getName, p1.getAge ) # Buzz 1
p1.setName('Sniffy')
p1.setAge(3.5)
print( p1.getName, p1.getAge ) # Sniffy 3.5
print( p1 ) # Sniffy is aged 3.5.
?? remove ))
Standard method, __str__()
The last line of code in previous lesson:
print( p1 )
prints out a message:
Sniffy is aged 3.5
This envolved the last method we defined in Pet
class, the __str__()
method, look at it's name: the same naming convention as the constructor __init__
, there are two underscores before and after the name. It is common that Python uses this naming technique for special methods.
So, you guessed right, just like __init__()
, __str__
is another one of the many method names that have special significance in Python classes.
__str__( )
method is responsible for returning a string representation as defined by the class creator. It is a commonly used method in about every class for the users to be able to print the object.
It is required that the __str__
method create and return a string. print
automatically use this when printing the objects.
(( another Dress
class to demo how to use setter, getter and __str__
))
class Dress:
def __init__(self, color='white', size=0):
self.color = color
self.size = size
def setColor(self, color):
self.color = color
def setSize(self, size):
self.size = size
def getColor(self)
return self.color
def getSize(self):
return self.size
def __str__(self):
return "%s dress, size %d" % (self.color, self.size)
d1 = Dress()
print(d1) # white dress, size 0
d1.setColor('blue')
d1.setSize(8)
print(d1) # blue dress, size 8
d2 = Dress('purple', 5)
print(d2) # purple dress, size 5
d2.setSize(2)
print( d2.getSize() ) # 2
print(d2) # purple dress, size 2
See different ways to assign values to an instance.
d1
has default values at initialization (color 'white'
and size 0
), then through the setter, d1
gets new values (color 'blue'
and size 8
)
d2
got it's initialization values color 'purple'
and size 5
when it's created; later it's size
is changed to be 2
via the setter method.
__str__
returns a string for print(object)
to use to print out message purple dress, size 2