12.07.2015 Views

Think Python - Denison University

Think Python - Denison University

Think Python - Denison University

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

15.8. Glossary 151>>> hasattr(p, 'x')True>>> hasattr(p, 'z')FalseThe first argument can be any object; the second argument is a string that contains the name of theattribute.15.8 Glossaryclass: A user-defined type. A class definition creates anew classobject.class object: Anobjectthatcontainsinformationaboutauser-definedtype. Theclassobjectcanbeused tocreate instances of thetype.instance: An object that belongs toaclass.attribute: One of thenamed values associated withan object.embedded (object): Anobject that isstoredas an attributeof another object.shallow copy: To copy the contents of an object, including any references to embedded objects;implemented bythecopyfunction inthecopymodule.deep copy: To copy the contents of an object as well as any embedded objects, and any objectsembedded inthem, and soon; implemented bythedeepcopyfunction inthecopymodule.object diagram: A diagram that shows objects, their attributes, and thevalues of the attributes.15.9 ExercisesExercise15.4 World.py,whichispartofSwampy(seeChapter4),containsaclassdefinitionforauser-defined type calledWorld. You can import itlikethis:from World import WorldThis version of theimport statement imports theWorld class from theWorld module. The followingcodecreates aWorldobject and callsthemainloopmethod, which waits forthe user.world = World()world.mainloop()A window should appear with a title bar and an empty square. We will use this window to drawPoints, Rectangles and other shapes. Add the following lines before calling mainloop and run theprogram again.canvas = world.ca(width=500, height=500, background='white')bbox = [[-150,-100], [150, 100]]canvas.rectangle(bbox, outline='black', width=2, fill='green4')

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!