General programming question. When to use OOP?

后端 未结 6 2196
没有蜡笔的小新
没有蜡笔的小新 2021-02-11 03:44

My program needs to do 2 things.

  1. Extract stuff from a webpage.

  2. Do stuff with a webpage.

However, there are many webpages, su

6条回答
  •  时光说笑
    2021-02-11 04:35

    Put as much of the common stuff together in a single function. Once you've factored as much out as possible, build a mechanism for branching to the appropriate function for each website.

    One possible way to do this is with python's if/else clauses, but if you have many such functions, you may want something more elegant such as

    F = __import__('yourproject.facebookmodule')

    This lets you put the code that's specific for facebook in it's own area. Since you pass __import__() a string, you can modify that at runtime based on which site you're accessing, and then just call function F in your generic worker code.

    More on that here: http://effbot.org/zone/import-confusion.htm

提交回复
热议问题