利用者:Kilon/Python book of magic/09.Object

提供: wiki
< 利用者:Kilon‎ | Python book of magic
2018年6月29日 (金) 05:44時点におけるYamyam (トーク | 投稿記録)による版 (1版 をインポートしました)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索

Contents | Previous | Next


9 Objects

We have already talked about objects in Chapter 4 Lists when we talked about "".join in strings. But we have not really explained what is an Object and why we need it. Learning why is crucial because the Blender Python API makes heavy usage of them

Objects like functions are a way to group things, both data (variables) and functions

9.1 Definition


class x:
    def __init__(self):
        self.a = 10
        self.b = 20

    def add_together(self):
        print("a + b = ",(self.a + self.b))

y = x()
y.add_together()

n = x()
n.a = 20
n.add_together()

Like <def> command for functions, <class> command is used here to define a class. A class is the blueprint of an object. What class tells python is how exactly each object ( also called "class instance" ) derived from this class should be constructed, which function it should include and which variable with which values.

In this case we define a class named <x>. This class has 2 functions. a) __init__() b)add_together()

__init__() is very important function used by classes to define instance variables known as "attributes". An instance is basically an object made using a class.

The second function is add_together() and the only thing it does is to add the 2 attributes together and prints out the sum.

y=x() may seem abit weird cause we have not really defined a x() function.But what really happens in this case is that x.__init__() is called. __init__ is actually the heart of an object and what makes objects so useful. They are called "constructor" in Object Orientated Programming for a very specific reason. It is called when an class instance is created and in this case it creates the instance variables. Instance variables are regular variables, the only special thing is that they are separate from other instance's variables.

We can create as many instances from the class x and we will create also variables that they will belong to those instances and they will not be affected by the variables of the same name in another instance.

So in this case even if we change a to 20 in the n instance, a remains 10 in the y instance. This way we isolate our values and variables without using new names for each instance. Isn't this fun?

The <self> command here represents the instance. Its mandatory when we create a new function definition inside a class definition that the first parameter is <self>. Of course our functions can have as many parameters we want as we have described in the previous chapter. But as you can see we don't use <self> when we call an instance's function, nor when we access its variables.

<self> is used however to access the instance variables inside the class definition as the above example illustrates.

9.2 Why we need Object Orientated Programming?


Object Orientated Programming also called OOP is basically the type of programming that is based around the creation of classes and class instances, in short object creation. Bare in mind that even classes are objects, which means that they have their own functions (of course outside the ones that defined in class definition) that helps us manage and modify classes.

One may wonder why its necessary to use OOP to complicate our lives. Well it happens that it is not. Python allow you to do only procedural programming (use only functions) or OOP or even mix the two, its completely up to you. However Blender uses OOP heavily for the Blender Python API. So its absolutely crucial that we understand it before proceeding.

OOP generally is not desirable for programs of few hundreds lines of code. However when programs get bigger, it becomes more helpful to divide and sort data and functions into small manageable groups and this is one of things that OOP can do for us. Blender as you can imagine is rather big program, over a million lines of code so OOP can really make life easier here. However this is not the only reason.

9.3 Inheritance


A neat feature of OOP is "inheritance". Its a very formal / scary word for something that is nothing more than a glorified "copy and paste" feature inside our source code.

class x:
    def one(self):
        print("I do one thing")

    def another(self):
        print("I do another thing")

class x_child(x):
    def one_more(self):
        print("I do more than one thing")

    def another_more(self):
        print("I do more than another thing")

y = x_child()
y.one()
y.another()
y.one_more()
y.another_more()

The first thing that probably your eye will notice is that weird parentheses next to the class x_child command. Till now we learned that parentheses are used for both defining and calling functions but this is an exception. What class x_child is doing here is inheriting from class x. Or to be precise it copies x contents to x_child, including class definitions and any instance variables (but we have no instance variables in this example). Its not truly "copy n paste" as you will find it in a text editor but I think you get the idea.

That is why when we create the y instance from the x_child class we are able to call all of x functions as well as x_child functions.

Inheritance is a big deal in OOP because it allows us to abstract. That is we can make classes that implement general functions and child classes that can implement more specific functions. This way we create tree like hierarchies that keep everything neatly organised and its hard to get lost inside our own source code. This is what the Blender Python API does alot. You are going to see in the next part when we start creating our own addons that hierarchies play a huge role inside Blender and every part is nothing more than a branch of a big tree.

9.4 Summary


What I want you to keep from all this, is the following

  • Objects are a neat way to group variables and functions
  • An Object needs a class to be created, also called "class instance"
  • A class is a blueprint of the object, telling python how to make the object
  • A class can define instance variables
  • Instance variables of the same name can have different values in each instance and will be independent.
  • You need to use the self word when defining class functions and instance variables and when you access instance variables from inside the class
  • An instance is created when we call a class as if it was a function
  • we can access instance variables and functions using the dot <.> sign
  • Inheritance allows our classes to share instance variables and class functions

Is that all we can learn for OOP? Far from it, we only touched the surface but we know enough to start making our addons. We are going to talk about OOP in the next part when we see OOP in practice. The only thing that remains for Part I is an introduction to the concept of modules. Here we go !



Contents | Previous | Next