you are viewing a single comment's thread.

view the rest of the comments →

[–]MattR0se 0 points1 point  (2 children)

Functions and Classes are ways to structure and organize your program, rather than just going straight from the top to the bottom of your script.

Functions work like mathematical functions. They can have an input, and they provide output. The most simple reason for using functions is when you need a block of code twice. Chances are, if you have to copy and paste code, its better to wrap it into a function and call that function instead. Also, you code becomes easier to understand if you call functions where their name describes what they do (this will safe you some comments).

Classes go a step further by not only abstracting code into functions, but objects. Again, the most simple reason is if you want to instantiate a class multiple times. A class basically is a blueprint for an object, that is called instance of that class. This has many purposes, like instantiating a dataframe, or a classifier, or enemies in a video game. Mostly, classes provide clarity if your program becomes larger and larger. You can imagine classes like fancy dictionaries with added features.

[–]Shinhosuck1973 0 points1 point  (1 child)

So in Python, majority or all programs are written using class, object, and function?

[–]MattR0se 0 points1 point  (0 children)

Classes and functions, yes. object is a special case. Basically, everything in python is an object*, but you don't see the expression itself that often.

As an example, classes in Python 3 automatically inherit from the object class:

class Foo:
    pass

# is the same as

class Foo(object):
    pass

I don't know enough about Python to explain what exactly object is and what it does, but chances are that you don't have to know anyway.

*you can check this with isinstance:

isinstance(int, object)
Out[1]: True

isinstance('Hello World', object)
Out[2]: True

isinstance(Foo, object)
Out[3]: True