Menu
Home
Log in / Register
 
Home arrow Computer Science arrow Data Structures and Algorithms with Python
< Prev   CONTENTS   Next >

3.8 Using Type Reflection

Many of the similarities in the two functions of Sects. 3.7.3 and 3.7.2 are due to operator overloading in Python. Python has another very nice feature called reflection. Reflection refers to the ability for code to be able to examine attributes about objects that might be passed as parameters to a function. One interesting aspect of reflection is the ability to see what the type of an object is. If we write type(obj) then Python will return an object which represents the type of obj. For instance, if obj is a reference to a string, then Python will return the str type object. Further, if we write str() we get a string which is the empty string. In other words, writing str() is the same thing as writing “”. Likewise, writing list() is the same thing as writing []. Using reflection, we can write one recursive reverse function that will work for strings, lists, and any other sequence that supports slicing and concatenation. A recursive version of reverse that will reverse both strings and lists is provided in Sect. 3.8.1.

3.8.1 Reflection Reverse

1 def reverse(seq):

2 SeqType = type(seq)

3 emptySeq = SeqType()

4

5 if seq == emptySeq:

6 return emptySeq

7

8 restrev = reverse(seq[1:])

9 first = seq[0:1]

10

11 # Now put the pieces together.

12 result = restrev + first

13

14 return result

15

16

17 def main():

18 print(reverse([1,2,3,4]))

19 print(reverse("hello"))

20 if name == " main ":

21 main()

After writing the code in Sect. 3.8.1 we have a polymorphic reverse function that will work to reverse any sequence. It is polymorphic due to reflection and operator overloading. Pretty neat stuff!

3.9 Chapter Summary

In this chapter, you were introduced to some concepts that are important to your understanding of algorithms to be presented later in this text. Understanding how the run-time stack and the heap work to make it possible to call functions in our programs will make you a better programmer. Forming a mental model of how our code works makes it possible to predict what our code will do. Writing recursive functions is also a skill that is important to computer programmers. Here is what you should have learned in this chapter. You should:

• be able to identify the various scopes within a program.

• be able to identify which scope a variable reference belongs to: the local, enclosing, global, or built-in scope. Remember the LEGB rule.

• be able to trace the execution of a program by drawing a picture of the run-time stack and the heap for a program as it executes.

• be able to write a simple recursive function by writing a base case and a recursive case where the function is called with a smaller value.

• be able to trace the execution of a recursive function, showing the run-time stack and heap as it executes.

• understand a little about reflection as it relates to examining types in Python code.

3.10 Review Questions

Answer these short answer, multiple choice, and true/false questions to test your mastery of the chapter.

1. What is an interpreter?

2. What is the Python interpreter called?

3. When the Python interpreter sees an identifier, in which scope does it look for the identifier first?

4. What order are the various scopes inspected to see where or if a variable is defined?

5. Pick a sample program from among the programs you have written, preferably a short one, and identify three scopes within it by drawing a box around the scopes.

6. When is an activation record pushed onto the run-time stack?

7. When is an activation record popped from the run-time stack?

8. What goes in the Heap in a computer?

9. What goes in an activation record on the run-time stack?

10. When writing a recursive function, what are the two cases for which you must write code?

11. If a recursive function did not have a base case, what would happen when it was called?

12. What must be true of the recursive call in a recursive function? In other words, what must you ensure when making this recursive call?

13. What does the type function return in Python? If you call the type function in a program, what aspect of Python are you using?

 
Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
 
Subjects
Accounting
Business & Finance
Communication
Computer Science
Economics
Education
Engineering
Environment
Geography
Health
History
Language & Literature
Law
Management
Marketing
Philosophy
Political science
Psychology
Religion
Sociology
Travel