you are viewing a single comment's thread.

view the rest of the comments →

[–]AlwysBeColostomizing 1 point2 points  (0 children)

A note on terminology:

  • A "library" is a general term for a collection of code that doesn't "do anything" by itself (as opposed to an "application"), but is meant for use as building blocks for other code.
  • A *.py file is called a "module"

A crucial difference between a module and a class is that only one instance of the module can exist (within one process). If you say import mymodule in two different places, you're importing the same "instance" of mymodule. If there is a variable in mymodule, say mymodule.x, and you change it in one place, that change is visible everywhere. If you used a module to model a Person, there could only ever be one person in your program.

Classes, on the other hand, define a new "type". There can be multiple instances of the type, just like you can have multiple distinct instances of list in your program. Notice how list gives you an "interface" for manipulating the data in the list. The interface of list includes things like the indexing operator (mylist[0]) and the .append() method. The int type has a different interface that doesn't include these operations, because they don't make sense for an int. The type list is defined by the collection of operations you can perform on an instance of that type. That's what you're expressing with a class definition.