What does abstraction mean in programming?

2019-01-20 23:38发布

I'm learning python and I'm not sure of understanding the following statement : "The function (including its name) can capture our mental chunking, or abstraction, of the problem."

It's the part that is in bold that I don't understand the meaning in terms of programming. The quote comes from http://www.openbookproject.net/thinkcs/python/english3e/functions.html

How to think like a computer scientist, 3 edition.

Thank you !

7条回答
在下西门庆
2楼-- · 2019-01-21 00:09

The best way to to describe something is to use examples:

A function is nothing more than a series of commands to get stuff done. Basically you can organize a chunk of code that does a single thing. That single thing can get re-used over and over and over through your program.

Now that your function does this thing, you should name it so that it's instantly identifiable as to what it does. Once you have named it you can re-use it all over the place by simply calling it's name.

def bark():
  print "woof!"

Then to use that function you can just do something like:

bark();

What happens if we wanted this to bark 4 times? Well you could write bark(); 4 times.

bark();
bark();
bark();
bark();

Or you could modify your function to accept some type of input, to change how it works.

def bark(times):
    i=0
    while i < times:
        i = i + 1
        print "woof"

Then we could just call it once:

bark(4);

When we start talking about Object Oriented Programming (OOP) abstraction means something different. You'll discover that part later :)

查看更多
登录 后发表回答