Intro to
Programming
(with Python)

Fall 2019
course
site

Thu Sep 13

Questions about anything so far? (Don't be shy ...)

I've given feedback on your homework ... please take a look.

Homework for next Tuesday is posted ... check it out.

Concepts from chap 4 - objects & graphics

another graphics example

Browse through Zelle's graphics API and discuss at an example : triangle.py .

(Notice that all the code in Zelle's book is downloadable ... check out the links at the left.)

a possible "gotcha" : two different names can refer to the same object

## Incorrect way to create two circles. 
leftEye = Circle (Point (80 , 50) , 5) 
leftEye.setFill('yellow')
leftEye.setOutline('red')
rightEye = leftEye          # OOPS
rightEye.move (20, 0)
name             value
----             -----
leftEye  --->    <class Circle>
rightEye -----------^

For a similar example, try this code in the pythonTutor :

a = [1,2,3]
b = [1,2,3]
c = b
#  What does a table of variable names and variable values look like?
#
# Here's a hint. (Look up the python id() function.)
print("id(a) = ", id(a))
print("id(b) = ", id(b))
print("id(c) = ", id(c))

copy'n'paste

## This works but has a "code smell" (google it) : "duplicated code"
leftEye = Circle (Point (80 , 50) , 5) 
leftEye.setFill('yellow')
leftEye.setOutline('red')

rightEye = Circle (Point (100 , 50) , 5) 
rightEye.setFill('yellow')
rightEye.setOutline('red')

clone

# A better way.
leftEye = Circle (Point (80 , 50) , 5) 
leftEye.setFill('yellow')
leftEye.setOutline('red')

rightEye = leftEye.clone()  # make a copy with same position, color, etc.
rightEye.move(20, 0)

future value graph

Look at, run, and discuss these programs from the textbook.

Things to understand :

more practice (depending on time)

aside

Why eval is evil ...

If this some python code ...

x = eval(input("What is your favorite color? "))

and you at the prompt you type for example

print(open('/etc/hosts').read())

what happens?

So this is a big security hole. Whoever runs that program can type any python - what the type could read for example read something private on your computer and send it over the internet to someone. Or delete your files with something like import os;os.remove('*')

Not good.

One of the basic notions of computer security is that when you are writing code, you need to make sure that information coming from the outside - things people type, files that you download from the internet, whatever - cannot be run as code that can do malicious things. eval(string) does exactly that ... so you should only use it when you know that string cannot contain something bad.

For a program that you run yourself, that you type at ... sure, eval() is fine. But the danger is that if you think this is an OK way to code, and you put in in (say) a public facing website, then you've just allowed someone to hack your computer.

That's why you should use int() or float() to convert a string to an integer or a decimal value, not eval().

another aside

"Gee Jim, doesn't this code that you showed us"

a = [1,2,3]
b = [1,2,3]
c = b
#  What does a table of variable names and variable values look like?
#
# Here's a hint. (Look up the python id() function.)
print("id(a) = ", id(a))
print("id(b) = ", id(b))
print("id(c) = ", id(c))

"have that 'code repetition' odor that you complained about earlier?"

Well, yes, yes it does. I'm glad you asked.

Here's some code would get rid of the that smell, which generalizes to lots more than three things ... at the cost of increased coding sophistication.

a = [1,2,3]
b = [1,2,3]
c = b
#
for name in ['a', 'b', 'c']:
    thing = eval(name)
    print( "id(", name, ") =  ", id(thing) )

That may well be a bit much for first-time coders ... but if you you put it into the pythontutor visualization tool, you can see what's going on. And it only uses stuff that we've talked about. :)

In general there's can often be a trade off between doing something simple, like cut'n'paste, or doing the same thing in a more powerful, more easily expandable way. The choice depends on lots of factors including your coding fluency and how likely it is to grow bigger later.

https://cs.marlboro.college /cours /fall2019 /python /notes /chap4b
last modified Fri April 26 2024 7:43 am