OOP in Python (argparse)


There are more complex OOP usage cases that you have surely encountered already in Python. Either while programming or in my course. For example parsing the command line arguments using argparse.

Here we call the ArgumentParser() method of the argparse object to create an instance of the argparse.ArgumentParser class. Then we call the add_argument method a few times and the parse_args method. This returns an instance of the argparse.Namespace class.

So in fact you have already used OOP quite a lot while using various already existing classes and instances of those classes.

Now we are going to learn how can you create your own classes.


examples/oop/examples_argparse.py
import argparse

def get_args():
    print(type(argparse))            # <class 'module'>

    parser = argparse.ArgumentParser()
    print(parser.__class__)          # <class 'argparse.ArgumentParser'>
    print(parser.__class__.__name__) # ArgumentParser

    parser.add_argument('--name')
    parser.add_argument('--email')

    # print(dir(parser))
    # print( parser.format_help() )
    # parser.print_help()

    return parser.parse_args()

args = get_args()
print(args.__class__)          # <class 'argparse.Namespace'>
print(args.__class__.__name__) # Namespace

print(args.name)      # None