[Tutor] Object oriented design

jamie hu jamiehu17 at yandex.com
Mon Dec 21 19:17:48 EST 2015


   *
   *
   20.12.2015, 12:49, "Danny Yoo" <dyoo at hashcollision.org>:

     On Sat, Dec 19, 2015 at 4:48 PM, jamie hu <[1]jamiehu17 at yandex.com>
     wrote:

       ****I am starting with Python object oriented concepts and have
       difficulty in
       ****understanding object instantiation. Below is an example code that
       I am
       ****trying to think/implement. I can create a given student object
       based on
       ****given firstname, lastname and grade. How do I find all objects
       matching
       ****particular criteria and return them to caller? Do I need to
       iterate/select
       ****through some list/database and create Student objects again?

     In order to find something, that something needs to be accessible from
     "somewhere".

     In typical beginner programs, that "somewhere" is an in-memory
     collection, like a list or dictionary, as Alan suggests. I'd expect,
     for your purposes, that this is an appropriate representation for
     "somewhere".

     find_by_lastname needs to know about this "somewhere". You have a few
     options. 1. You can either pass the "somewhere" in as an explicit
     parameter, or 2. hardcode it within find_by_lastname's definition.

     We expect that #1 will look something like:

     #####################################
     """Collection of things."""
     STUDENTS = []

     ...

     def find_by_lastname(students, lastname):
     ****"""Contract: listof(Student) string -> Student
     ****Given a list of students and a student's last name,
     ****returns the student with that last name.
     ****"""
     ***# ... fill me in

     ## later, we can call find_by_lastname, passing in the
     ## collection as an explicit argument.
     find_by_lastname(STUDENTS, "jamie")
     #####################################

     And #2 will probably look something like:

     #####################################
     """Collection of things."""
     STUDENTS = []

     ...

     def find_by_lastname(lastname):
     ****"""Contract: string -> Student
     ****Given a student's last name, returns the student
     ****with that last name, looking through STUDENTS.
     ****"""
     ***# ... fill me in

     ## later, we can call find_by_lastname, passing in the
     ## collection as an explicit argument.
     find_by_lastname("jamie")
     #####################################

     This is a rough sketch. So, which one do you choose?

     I have no idea!

     This is a decision point, and one that you need to resolve, because
     either choice has its own advantages and tradeoffs. Assuming this is
     an assignment, you need to talk with your instructor to see if there's
     one that they had in mind, or if this is something you get to decide.
     Personally, I don't like hardcoding, so #1 is my pick, but #2 has its
     advantages too: it's easier to call.

     Please feel free to ask questions. Good luck.

   *
   *
   Thanks Danny. I was confused about what to return and How? Should I return
   user objects for User.find_by_lastname() method and do I need to
   re-instantiate objects?
   *
   I think storing them as a list in memory solves this issue. If I were to
   use database I will need to search and create object again while returning
   to user. Is that right?*
   *
   --
   Jamie
   *
   *

     *

References

   Visible links
   1. mailto:jamiehu17 at yandex.com


More information about the Tutor mailing list