Vector Class (Planning)
Multiple Uses:
- direction, direction and Magnitude
- Point in space
- Color (think about normal maps, its a collection of rotational data. The reason most of it is purple is because most of the times the object will be facing the viewer, the z value)
With vectors you can find the length (setattr), inversion, add, subtract, cross product (multiply), dot Product, Unit (Length of 1.0).
Unary Operators vs Binary Operators: Unary works with only one operand, while binary takes two operands. Unary operators are things like increment, decrement, not. Binary operators are things like addition, subtraction, multiplication.
When python encounters this line, python then asks how much memory does this consume. and goes and devotes a certain chunk of memory to it, and returns a pointer that points to the beginning of that piece of memory. So when you do print(v1), it returns the pointer value of that variable.
Then you can manipulate the values within the class. But what happens if a user wants to manipulate a variable that was not originally in the class? Well you could do that, and python will allocate memory for that variable.
You could allow this. However, if you want to stop users to not make extraneous values, you can stop them by using the setattr operation.
By default the setattr method gets three arguments; use this to keep law and order inside of your class if other users were to use this. Lets say we only want vname, xval, yval, and zval by itself. We can check if the variable name being set is either vname, xval, yval, or zval. If it is true, we allow the user to change the value. Else, we raise a ValueError and tell the user to no create a new attribute name. Now when we try to set v1.Amanda = 'TA', we get an error and can't run the script.
By default the setattr method gets three arguments; use this to keep law and order inside of your class if other users were to use this. Lets say we only want vname, xval, yval, and zval by itself. We can check if the variable name being set is either vname, xval, yval, or zval. If it is true, we allow the user to change the value. Else, we raise a ValueError and tell the user to no create a new attribute name. Now when we try to set v1.Amanda = 'TA', we get an error and can't run the script.
Python organizes data as dictionaries. Classes are just dictionaries, so that's why we use the 'self.__dict__[name] = value' for the setattr method. Whenever the class is called, it uses the variable name as a key to look up where the data is in memory.
If you do not have the __dict__ here and just write self.name =, then it will recursively call itself forever. This is because self.name = value calls the setattr function automatically.
Let's say that when you call print(v1), you want the print statement to say something else other than the pointer value. What you can do is create a string overload operation in the class.
This only tells us how to replace the string, not print, so we need to return a string, telling the class how to display itself. We also truncate it to just 3 decimal places.
From here we can do operator overloads on things like adding, subtracting, etc.
This part will return a new Vector 3 object when you go and add two Vector3 objects together. There are others you can do as well:
No comments:
Post a Comment