Skip to content

Latest commit

 

History

History
95 lines (64 loc) · 3.63 KB

File metadata and controls

95 lines (64 loc) · 3.63 KB

Objects/Instances

There are a series if key data types pre-defined (builtin) to Python. The key primaries: "int", "float", "str", "bool", "None".

And there are the "sequence" types: "list", "tuple", "range".

Several can also be thought of as "container" types: "list", "tuple","dict", "set", "frozenset".

All of these are actually Classes in Python. They are all built-in classes, and are available to use without any special syntax. And type() will tell you the class of an object. Any object at all.

Moreover, Python is an object oriented programming language. Everything in Python is an object, with its properties/attributes and methods. A Class is an object constructor, or a "blueprint" for creating objects.

So you write "classes", and then when you run the code, Python then creates the objects/instances to be used by the code.

Note
You’ll see both words used - object and instance - and they mean the same thing. When you create an object from a class, programmers usually say you’ve "created an instance of that class." So "object" and "instance" are really just two names for the same idea, and you’ll see both used throughout this book (and in Python’s own error messages).

Objects are used to collect and organize data - and that data can be variable values, functions and other things. Objects can also contain other objects(!), in kind of a "nesting" way. This allows for large data structures to be built using a very simple and elegant mechanism.

class Spacecraft:
    name = ""
    type = 'warp starship'
    topspeed = 9.9

Object Creation

We can imagine an object - an instance of a class - as a container where everything is collected about some thing in the program:

enterprise = Spacecraft()
enterprise.name = "Enterprise"

print(enterprise) # -> <__main__.Spacecraft object at 0x103013100>

Which isn’t very helpful, is it? We need to create a method in our class. One that gets called whenever we want to display which spacecraft to a user.

class Spacecraft:
    def __init__(self):
        self.name = ""
        self.type = 'warp starship'
        self.topspeed = 9.9
        self.current_speed = 0

    def __str__(self):
        return "Starship " + self.name

    def tp_speed(self):
        return self.topspeed

enterprise = Spacecraft()
enterprise.name = "Enterprise"

# now when we
print(enterprise)
# we get: Starship Enterprise

There a few things to point out in this class. First, notice how the functions are indented? Those functions are defined inside the class. That makes the function a method; objects have methods. You can call a method on a particular object.

Also notice that first parameter, self, on every method. self is how a method knows which instance it’s being called on - when you call enterprise.tp_speed(), Python quietly hands tp_speed the enterprise instance as self, so self.topspeed reaches into that specific instance’s data, not some other spacecraft’s.

enterprise = Spacecraft()
enterprise.name = "Enterprise"

constellation = Spacecraft()
constellation.name = "Constellation"
constellation.topspeed = 9.2

ets = enterprise.speed()
cts = constellation.speed()

# ets will be 9.9, cts will be 9.2

This example also shows how you use a class to create multiple objects. enterprise and constellation are two separate instances of Spacecraft - each one built from the very same blueprint, but each holding its own, independent set of values. Changing constellation.topspeed had no effect at all on enterprise. Using a class this way helps with making code simpler, and easier to follow when reading it.