How are classes better?

Imagine you have a program that calculates the velocity of a car in a two-dimensional plane using functions. If you want to make a new program that calculates the velocity of an airplane in three dimensions, you can use the concepts of your car functions to make the airplane model work, but you'll have to rewrite many of the functions to make them work for the vertical dimension, especially to map the object in a 3D space. You may be lucky and be able to copy and paste some of them, but for the most part, you'll have to redo much of the work.

Classes let you define an object once, and then reuse it multiple times. You can give it a base function (functions in classes are called methods to indicate that they are object-oriented) and then build upon that method to redefine it as necessary. It also lets you model real-world objects much better than using functions. In short, a class provides the basic template and default behavior for an object, and an instance of that class, that is, a particular incarnation created from the class, uses the base template as a foundation for more specific changes.

For example, you could make a tire class that defines the size of the tire, how much pressure it holds, what it's made of, and so on, and then make methods to determine how quickly it wears down based on certain conditions. You can then use this tire class as part of a car class, a bicycle class, or whatever. Each use of the tire class (called instances) would use different properties of the base tire object. If the base tire object said it was just made of rubber, perhaps the car class would "enhance" the tire by saying it had steel bands or maybe the bike class would say it had an internal air bladder. This will make more sense later.

Several concepts of classes are important to know:

  • Classes have a definite namespace, just like modules. Trying to call a class method from a different class will give you an error unless you qualify it using the dot protocol; for example, spamClass.eggMethod(). We have seen this when a module is imported, then we attempt to use a method from inside it. As we saw in Chapter 2Data Types and Modules, in the Types of imports section, for example, if you simply import math, you have to qualify anything that comes from it, such as math.log(). However, if you use from math import *, then you don't have to qualify it, as all those items are now part of your program's namespace. Of course, if you happen to make your own variables or methods with the same names, you'll run into problems due to variable shadowing.
  • Classes support multiple copies. This is because classes have two different object types: class objects and instance objects. Class objects give the default behavior and are used to create instance objects. Instance objects are the objects that actually do the work in your program; every time a class is called, a new instance is created. You can have as many instance objects of the same class object as you need. Instance objects are normally marked by the self keyword, so a class method could be Car.Brake(), while a specific instance of the Brake() method would be marked as self.Brake(). (This is covered in more depth in Chapter 4, Functions and Object Oriented Programming, in the Classes and Instances section.)
  • Each instance object has its own namespace but also inherits from the base class object. This means that each instance has the same default namespace components as the class object, but additionally, each instance can make new namespace objects just for itself. This is part of each instance being a new and separate object. In other words, an instance has objects that are part of the instance scope, but it also has access to the base class' scope.
  • Class objects are similar to any built-in variable in Python. So a list of Python objects can be sliced, indexed, concatenated, and so on, just like strings, lists, and other standard Python types. This is because everything in Python is actually a class object; we aren't actually doing anything new with classes, we're just learning how to better use the inherent nature of the Python language.

Here's a brief list of Python OOP concepts:

  • The class statement creates a class object and gives it a name, as well as creating a new namespace.
  • Variable assignments and methods within the class create class attributes. These attributes are accessed by qualifying the name using dot syntax: ClassName.Attribute.
  • Class attributes export the state of an object and its associated behavior. These attributes are shared by all instances of a class.
  • Calling a class creates a new instance of the class. This is where the multiple copies part comes in.
  • Each instance gets ("inherits") the default attributes of its class, while also getting its own namespace. This prevents instance objects from overlapping and confusing the program.
  • Using the term self identifies a particular instance, allowing for per-instance attributes. This allows items such as variables to be associated with a particular instance.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.122.82