Skip to main content

Posts

Advance Abstraction

In the previous tutorial, you looked at Python’s main built-in object types (numbers, strings, lists, tuples, and dictionaries); you peeked at the wealth of built-in functions and standard libraries; and you even created your own functions. Now, only one thing seems to be missing—making your own objects. And that’s what you do in this chapter.     You may wonder how useful this is. It might be cool to make your own kinds of objects, but what would you use them for? With all the dictionaries and sequences and numbers and strings available, can’t you just use them and make the functions do the job? Certainly, but making your own objects (and especially types or classes of objects) is a central concept in Python—so central, in fact, that Python is called an objectoriented language (along with Smalltalk, C++, Java, and many others). In this chapter, you learn how to make objects. You learn about polymorphism and encapsulation, methods and attributes, superclasses, and inher...

Abstraction | Core Python 3.8

In this chapter, you’ll learn how to group statements into functions, which enables you to tell the computer how to do something, and to tell it only once. You won’t need to give it the same detailed instructions over and over. The chapter provides a thorough introduction to parameters and scoping, and you’ll learn what recursion is and what it can do for your programs. Laziness Is a Virtue  The programs we’ve written so far have been pretty small, but if you want to make something bigger, you’ll soon run into trouble. Consider what happens if you have written some code in one place and need to use it in another place as well. For example, let’s say you wrote a snippet of code that computed some Fibonacci numbers (a series of numbers in which each number is the sum of the two previous ones). fibs = [0, 1]  for i in range(8):     fibs.append(fibs[-2] + fibs[-1]) After running this, fibs contains the first ten Fibonacci numbers. >>> fibs...