Question about methods and functions (string methods and functions specifically)

Contents of functionything.py

this is a function

def hurray_cars(): print("cars are neat!")

class Car(): model = ""

# this is a function of a class, also called a method
def turn(self, degrees, left_or_right):
    print("Turning {0} degrees {1}".format(degrees, left_or_right))

# another method
def report_car_type(self):
    print("I'm a {0}".format(self.model))

if name == "main": # call the hurray_cars() method hurray_cars()

# create an instance of the Car class
honda98 = Car()
honda98.model = "98 Honda"

# call the turn method of the honda98 instance of the Car class
honda98.turn(90, "left")

# call the report_car_type of the honda98 instance of the Car class
honda98.report_car_type()

Now, launch $ ipython and let's take a look at that .py file:

In [1]: import functionything

In [2]: dir(functionything)
Out[2]:
['Car',
 '__builtins__',
 '__doc__',
 '__file__',
 '__name__',
 '__package__',
 'hurray_cars']

In [3]: dir(functionything.Car)
Out[3]: ['__doc__', '__module__', 'model', 'report_car_type', 'turn']

Let's look at str: In [4]: dir(str) Out[4]: ['add', 'class', 'contains', 'delattr', 'doc', 'eq', 'format', 'ge', 'getattribute', 'getitem', 'getnewargs', 'getslice', 'gt', 'hash', 'init', 'le', 'len', 'lt', 'mod', 'mul', 'ne', 'new', 'reduce', 'reduce_ex', 'repr', 'rmod', 'rmul', 'setattr', 'sizeof', 'str', 'subclasshook', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

Does that help at all?

/r/learnpython Thread