some problems for an introductory python test

Mats Wichmann mats at wichmann.us
Mon Aug 9 18:14:14 EDT 2021


On 8/9/21 3:07 PM, Hope Rouselle wrote:
> I'm looking for questions to put on a test for students who never had
> any experience with programming, but have learned to use Python's
> procedures, default arguments, if-else, strings, tuples, lists and
> dictionaries.  (There's no OOP at all in this course.  Students don't
> even write ls.append(...).  They write list.append(ls, ...)).

Nitpickery... there *is* OOP in the course, they just don't know it.

Long long ago (over 20 yrs now) I developed a Python course for a 
commercial training provider, and in it I claimed one of the great 
things about Python was it supported all kinds of object oriented 
programming techniques, but you could also use it without doing anything 
object oriented. If I wrote a course now, I'd never make that claim, 
because everything you do in Python is pretty much object oriented.

 >>> x = list()
 >>> type(x)
<class 'list'>
 >>> dir(x)
['__add__', '__class__', '__class_getitem__', '__contains__', 
'__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', 
'__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', 
'__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', 
'__iter__', '__le__', '__len__', '
__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', 
'__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', 
'__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 
'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

list is a class and it has methods... it's "object-oriented"!


Even if you do

x = 2 + 3

you're actually creating an integer object with a value of 2, and 
calling its add method to add the integer object with the value of 3 to 
it. The syntax hides it, but in a way it's just convenience that it does 
so...

 >>> 2 + 3
5
 >>> x = 2
 >>> x.__add__(3)
5


sorry for nitpicking :)  But... don't be afraid of letting them know 
it's OOP, and it''s not huge and complex and scary!







More information about the Python-list mailing list