Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

Python 4 Kids: Python for Kids: Python 3 Project 9

$
0
0
python for Kids: Python 3 Project9

September 11, 2016 Leave a comment

Using Python 3 in Project 9 of Python For Kids For Dummies

In this post I talk about the changes that need to be made to the code of

Project 9 in order for it to work with Python 3. Most of the code in project 9 will work without changes.

However, in a lot of cases what Python outputs in Python 3 is different from the output in Python 2.7.

This project has a lot of code. To shorten the length of this post I am only showing the Python 3 versions of

the longer pieces, rather than both Python 2.7 (from the book) and Python 3.

Disclaimer

Some people want to use my book Python for Kids for Dummies to learn Python 3.

I am working through the code in the existing book, highlighting changes from Python 2 to Python 3

and providing code that will work in Python 3. If you are using Python 2.7 you can ignore this post.

This post is only for people who want to take the code in my book Python for Kids for Dummies and

run it in Python 3.

Page 240

All code on this page is the same, and all outputs from the code is the same in Python 3 as in Python 2.7.

However, in Python 3 you don’t need to add “(object)” when defining a class it is assumed.

It is not an error to add (object) in Python 3, it just doesn’t look as nice. For example, on this page a class is defined

using class AddressEntry(object): (in Python 2.7). In Python 3 it’s just class AddressEntry:

>>> class AddressEntry(object): """ AddressEntry instances hold and manage details of a person """ pass >>> AddressEntry # without parentheses <class '__main__.AddressEntry'> >>> >>> AddressEntry() # parentheses create an instance <__main__.AddressEntry object at 0x7f9309751590> >>> address_entry = AddressEntry() # Python 3 >>> class AddressEntry: # note: no (object) """ AddressEntry instances hold and manage details of a person """ pass >>> AddressEntry # without parentheses <class '__main__.AddressEntry'> >>> >>> AddressEntry() # parentheses create an instance <__main__.AddressEntry object at 0x7f9309751590> >>> address_entry = AddressEntry()

Page 241

All code on this page is the same, and all outputs from the code are the same in Python 3 as in Python 2.7.

Page 242

All code on this page is the same, and all outputs from the code is the same in Python 3 as in Python 2.7.

You still need to use () when creating instances of an object. That is, making an instance of a class, rather than

defining the class.

Page 244

All code on this page is the same, and all outputs from the code is the same in Python 3 as in Python 2.7

However, in Python 3 you don’t need to add “(object)” when defining a class it is assumed.

It is not an error to add (object) in Python 3, it just doesn’t look as nice.

Python 2.7 """ Addressbook.py An address book program to store details of people I know. Stuff I'm storing is: first name family name email address date of birth [other stuff] Brendan Scott Feb 2015 """ ##### Classes Section class AddressBook(object): """ AddressBook instances hold and manage a list of people """ pass class AddressEntry(object): """ AddressEntry instances hold and manage details of a person """ ##### Main Section if __name__ == "__main__": address_book = AddressBook() person1 = AddressEntry() # Python3 """ Addressbook.py An address book program to store details of people I know. Stuff I'm storing is: first name family name email address date of birth [other stuff] Brendan Scott Feb 2015 """ ##### Classes Section class AddressBook: """ AddressBook instances hold and manage a list of people """ pass class AddressEntry: """ AddressEntry instances hold and manage details of a person """ ##### Main Section if __name__ == "__main__": address_book = AddressBook() person1 = AddressEntry()

Page 245

All code on this page is the same, and all outputs from the code are the same in Python 3 as in Python 2.7.

Page 246

All code on this page is the same, and all outputs from the code is the same in Python 3 as in Python 2.7.

However, in Python 3 you don’t need to add “(object)” when defining a class it is assumed.

It is not an error to add (object) in Python 3, it just doesn’t look as nice.

#Python2.7 class AddressEntry(object): """ AddressEntry instances hold and manage details of a person """ def __init__(self, first_name=None, family_name=None, email_address=None, date_of_birth=None): """Initialize attributes first_name, family_name and date_of_birth. Each argument should be a string. date_of_birth should be of the form "MM DD, YYYY" """ self.first_name = first_name self.family_name = family_name self.email_address = email_address self.date_of_birth = date_of_birth #Python3 class AddressEntry: """ AddressEntry instances hold and manage details of a person """ def __init__(self, first_name=None, family_name=None, email_address=None, date_of_birth=None): """Initialize attributes first_name, family_name and date_of_birth. Each argument should be a string. date_of_birth should be of the form "MM DD, YYYY" """ self.first_name = first_name self.family_name = family_name self.email_address = email_address self.date_of_birth = date_of_birth

Page 248

All code on this page is the same, and all outputs from the code is the same in Python 3 as in Python 2.7.

However, in Python 3 you don’t need to add “(object)” when defining a class it is assumed.

It is not an error to add (object) in Python 3, it just doesn’t look as nice.

############Python 2.7 ##### Classes Section class AddressBook(object): """ AddressBook instances hold and manage a list of people """ pass class AddressEntry(object): """ AddressEntry instances hold and manage details of a person """ def __init__(self, first_name=None, family_name=None, email_address=None, date_of_birth=None): """Initialize attributes first_name, family_name and date_of_birth. Each argument should be a string. date_of_birth should be of the form "MM DD, YYYY" """ self.first_name = first_name self.family_name = family_name self.email_address = email_address self.date_of_birth = date_of_birth ##### Main Section if __name__ == "__main__": address_book = AddressBook() person1 = AddressEntry("Eric", "Idle", None, "March 29, 1943") print(person1) ############Python 3 ##### Classes Section class AddressBook: """ AddressBook instances hold and manage a list of people """ pass class AddressEntry: """ Addres

Viewing all articles
Browse latest Browse all 9596

Trending Articles