Python Forum
Best approach before adding features - Movie information script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Best approach before adding features - Movie information script
#2
Hi pythonnewbie138, here's a couple ideas that might help you:

Instead of using class attributes, you might consider creating the attributes inside the __init__ method:

#try this
class movie_db_search:
    def __init__(self):
        self.tmdb_id = ""
        self.tmdb_url = "https://www.themoviedb.org/movie/" 
        ...
        self.genres = [] #instance attribute

#instead of this
class movie_db_search:
    tmdb_id = "" 
    tmdb_url = "https://www.themoviedb.org/movie/"
    ...
    genres = [] #class attribute
__init__ is one of python's "magic methods", which just means it's a method that python calls automatically. In the case of __init__, python calls it whenever you create a new instance. In your example you're adding the attributes to the class object, which means they will be shared. This is fine for anything that is "passed by copy" (C++ concept) such as strings and ints, but the attributes that are "passed by reference" such as lists and dictionaries, can be mutated by any other instance of the class. There are times you might want this behavior, for instance if your code needed to retrieve a session handle, you might choose to do that once at the class level, and let each instance use it that way.

You should also get in the practice of putting all of your calls at the end of the module under a condition like this:

if __name__ == '__main__':
    name_loc_data = names_locations()
 
    #print(name_loc_data.path)
    #print(name_loc_data.dest)
    #print(name_loc_data.basename)
    ...
This will make sure these lines only run if you're running this module directly ie:
>> python movie_db_search.py

Once you include this check, it will allow you to import this module into another module so that you can access your class without running these extra lines.

I usually like to take it one step further and put all of it in a main function like this:

def main():
    name_loc_data = names_locations()
 
    #print(name_loc_data.path)
    #print(name_loc_data.dest)
    #print(name_loc_data.basename)
    ...

if __name__ == '__main__':
    main()
Reply


Messages In This Thread
RE: Best approach before adding features - Movie information script - by MadisonAster - Oct-15-2020, 08:40 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020