Tech With Tim Logo
Go back

Introduction to Objects

What is an Object?

In python almost everything we create and use is an object. Pretty much any time we declare a variable we are creating a new object of a certain class. Different objects have certain properties and they inherit those properties from the class they belong to. For example objects of type str have methods like .strip() and .split(), Integers can be added and lists can be indexed. These are all proprties specific to objects of certain classes.

In this series I will showing you how we can create our own classes and objects.

Help() Function

To be able to have a look at some of the builtin functions in python we can use the help() function. The help function will list all of the methods and attributes of a class and give us descriptions on what everything does.

help(int)

# Try running this and see what happens

Definitions

Instance: Whenever we create a new object we are said to be creating an instance of a class. For example typing the command x = 1 translates to: creating a new instance of the int class with the value 1 and the name x.

Method: You can think of a method as a function that is specific to certain objects and classes. Methods are created within a class and are only visible to instances of that class. An example of a method is .strip(). It can only be used on objects of class str as it is specific to the str class. All methods must be called on an instance of a class. We cannot simply type strip() as we need to include an instance followed by a period before it.

Attributes: An attribute is anything that is specific to a certain object. For example the object has an attribute color. We can change that color and modify it and if we create a new turtle object it can have a different color.

Design & Development by Ibezio Logo