How do I design a class in Python?

后端 未结 6 1688
终归单人心
终归单人心 2021-01-29 16:59

I\'ve had some really awesome help on my previous questions for detecting paws and toes within a paw, but all these solutions only work for one measurement at a time.

No

6条回答
  •  耶瑟儿~
    2021-01-29 17:37

    The whole idea of OO design is to make your code map to your problem, so when, for example, you want the first footstep of a dog, you do something like:

    dog.footstep(0)
    

    Now, it may be that for your case you need to read in your raw data file and compute the footstep locations. All this could be hidden in the footstep() function so that it only happens once. Something like:

     class Dog:
       def __init__(self):
         self._footsteps=None 
       def footstep(self,n):
         if not self._footsteps:
            self.readInFootsteps(...)
         return self._footsteps[n]
    

    [This is now a sort of caching pattern. The first time it goes and reads the footstep data, subsequent times it just gets it from self._footsteps.]

    But yes, getting OO design right can be tricky. Think more about the things you want to do to your data, and that will inform what methods you'll need to apply to what classes.

提交回复
热议问题