We’ve looked at working with a single object. But what about real life situations, where you need multiple objects?
For example, as Python Object of a single student is nice, but you need to have a group of them to be really useful. This also works for other objects, like cars on a dealership lot, or items in a store waiting to be sold.
Let’s take our previous class example of a dice item, and work with having multiple objects. Now, we could do something like:
def main() :
my_dice1 = dice.Dice() # create an instance of the dice
my_dice2 = dice.Dice() # create an instance of the dice
my_dice3 = dice.Dice() # create an instance of the dice
my_dice4 = dice.Dice() # create an instance of the dice
my_dice5 = dice.Dice() # create an instance of the dice
for num in range(25) :
my_dice1.roll()
my_dice2.roll()
my_dice3.roll()
my_dice4.roll()
my_dice5.roll()
print('Roll', str(num +1) + ':', my_dice1.get_value(),',', my_dice2.get_value(),',', my_dice3.get_value(),',', my_dice4.get_value(),',', my_dice5.get_value() )
main()
Here we use our previously defined dice class, and create 5 instances of it. However, this code both looks messy, and is hard to work with.
While you can create a unique instance variable of each object, could you imagine having a thousand products in your catalog?
import dice
def main() :
my_die = [dice.Dice()] * 5 # create a list of five dice objects
for num in range(25) :
print('Roll', str(num +1) + ':', end = ' ')
for one_dice in my_die :
# use the nested loop to go through each dice in the list
one_dice.roll()
print(one_dice.get_value(), end = ', ')
print()
main()
Printing an Object
If you try to print an object, you run into an issue in that it will print the class type, and a weird hexadecimal number. This is it’s memory location.
<dice.Dice object at 0x0000004493121198>
<dice.Dice object at 0x0000004493121198>
<dice.Dice object at 0x0000004493121198>
<dice.Dice object at 0x0000004493121198>
<dice.Dice object at 0x0000004493121198>
You can create a special method called __str__() which will print a nicer string value if you try to print the object. The BankAccount example in the book does a nice job of this.
def __str__(self) :
return "Dice: " + self.__value
Notice that you return the string. This way you control what data is, but not how it is to be presented, in case it isn’t to be presented to the screen. Maybe it is sent to a web application, or database.
The actual request for data, and printing data should be handled by the main application – not the class. Always!
Passing Data to the Constructor
For more complex data, it is common to pass data to a constructor when the object is created.
Consider the class below:
class Contact :
def __init__ (self, name, phone, email) :
self.__name = name
self.__phone = phone
self.__email = email
Then, in the main body of the application, you’d see something like:
person_name = input("What is their name?")
phone_number = input("What is the phone number?")
email_add = input("What is the email address?")
contact = Contact(person_name, phone_number, email_add)
Notice that while the argument and parameter names do not have to match, they values do correlate to each other.
Working with Python Objects was originally found on Access 2 Learn