General programming question. When to use OOP?

后端 未结 6 2227
没有蜡笔的小新
没有蜡笔的小新 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:25

    It's up to you. I personally try to stay away from Java-style classes when programming in python. Instead, I use dicts and/or simple objects.

    For instance, after defining these functions (the ones you defined in the question), I'd create a simple dict, maybe like this:

    { 'facebook' : { 'process' : facebookProcess, 'extract': facebookExtract }, 
     ..... 
    }
    

    or, better yet, use introspection to get the process/extract function automatically:

    def processor(sitename):
        return getattr(module, sitename + 'Process')
    
    def extractor(sitename):
        return getattr(module, sitename + 'Extractor')
    

    Where module is the current module (or the module that has these functions).

    To get this module as an object:

    import sys
    module = sys.modules[__name__]
    

    Assuming of course, that the generic main function does something like this:

        figure out sitename based on input.
        get the extractor function for the site
        get processor function for the site
        call the extractor then the processor
    

提交回复
热议问题