cosmogonies.net Blog of cosmogonies.net

suggest
handbook
20Jul/120

APyTA_15 : OBJECT Part I, Everything is an object, and Object is everything, and…

APyTA_15 : OBJECT Part I, Everything is an OBJECT is Everything...

The golden rule in python is this. Everything is an object, so without fully understand it, you already use it a lot.
You may also already known that python is an object-oriented language.
It is time to define this notion right now.

A class is a definition of a template, describing a structure. A type.
An object is an instance of this class, its application into a 'real' case

MyVar = "lalo"
type(MyVar) # Result: <type 'str'> #
MyVar.__len__() # Result: 4 #
id(MyVar)# Result: 875904576L #
MyVar.replace("la","ri") # Result: 'rilo' #

Looking at this little piece of code, you have to understand that the type statement will return the name of the class, given the object.
Every time you create a variable, using the = operator, python will store somewhere in RAM (indexed by id() ) a copy of the class definition, with your given values.
This is the key concept here, the instantiation. And forget about Maya Instances, the concept here is different.
To take a more accurate example, Instantiate a class into a variable is like copy/paste a template folder, where you already put some subFolder/subFiles to avoid to do that every time.
Storing a named variable with a typed-value is precisely instantiate a class, given its template as a type, to create a new object.

The next notion, and sorry again, some vocabulary to learn, is to know what classes are made of.
Basically, they are a list of Members and Methods.
Members are local variable, created in the scope of the instance of the class, and refers to a value (could be a built-in, as int or string, but could be any other classes too).
Methods are like members, created locally in the object-scope, but they are functions defined with a def statement, they have a scope,  and can return value.

Python does not comes with built-in variables type to manage Vectors. Maya does, but forget that and focus on how to do some calculus with vectors.
First, you can create a variable, setting it with a tuple of three floats, like MyVector = (1.2,3.4,5.6). But you will very soon have readability issues to access the Y axis by MyVector[1] ...
Secondly, you will have to create a lot of calculus functions, and every time you will need to assert your arguments, because they will work only for your specific tuple.

import math
class APYTA_Vector():

  def __init__(self,_x=0.0,_y=0.0,_z=0.0):
    self.x=_x
    self.y=_y
    self.z=_z

  def getLength(self):
    return math.sqrt( self.x*self.x + self.y*self.y + self.z*self.z)

  def Normalize(self):
    InvLength = 1 / self.getLength()
    self.x *= InvLength
    self.y *= InvLength
    self.z *= InvLength

print APYTA_Vector() #<__main__.APYTA_Vector instance at 0x000000000CF23B88>
MyVector = APYTA_Vector(1,2,3)
print MyVector.y #2
print MyVector.getLength()#3.74165738677
MyVector.Normalize()
print MyVector.x, MyVector.y, MyVector.z #0.267261241912 0.534522483825 0.801783725737
print MyVector.getLength()#1.0

First, we can see here the class is defined by the keyword class. I usually had a prefix to my classes, because clashes can occurs, and that signed them explicitly.
The members of the class are defined into a very specific function (a Method) __init__(self). Here I setup three members, initialized at 0.0
Remember well the self , which is mandatory to be the first argument, and it explicitly refers to the Instance of the class (the object) .
This __init__ function is not really a constructor like in other languages (if you omit it, your object still exist), but it is used for initialization, declaration of members.

Like __init__, you can define any functions, with def statement, into the scope of the class. It is exactly like a function, living in the scope class, and having a first mandatory argument self.
Here, the method getLength can return an evaluation of all the object members, and see how referring to it is so neat : MyVector.getLength() .
This is the "object revolution" in coding. You will start to forget about processes that take "that and that" and return its calculus.
You will begin to manage object of meaning. Define functions (Methods) that just make sense.

Finally, see how I can modify the members of my object into a method, like in Normalize() ?
Nothing is returned, but the values (members) of my current instance are updated according to the processes of Normalization.
It is the very same thing of a list.sort() for example.

profile
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

store

No trackbacks yet.

store