In [ ]:
len((3, 34, 3))
len('sfdjfdjfkd')

In [ ]:
class Point(object):
    '''This is a Point in 2/3D space.
    You can use .u ro .v for a uv point'''
    
    def __init__(self, x=0, y=0, z=0):
        self.x = x
        self.y = y
        self.z = z
        
    def __len__(self):
        return 3
        
    def __getattribute__(self, name):
        if name == 'u':
            return self.x
        elif name == 'v':
            return self.y
            
        return super(Point, self).__getattribute__(name)
    
    def __setattr__(self, name, value):
        if name == 'u':
            self.x = value
        elif name == 'v':
            self.y = value
            
        super(Point, self).__setattr__(name, value)
    
    def __mul__(self, other):
        if isinstance(other, int) or isinstance(other, float):
            self.x *= other
            self.y *= other
            self.z *= other
        else:
            raise TypeError("must mul with int or float type")
            
        return self
            
    def __rmul__(self, other):
        return self * other
    
    def __div__(self, other):
        if isinstance(other, int) or isinstance(other, float):
            self.x /= other
            self.y /= other
            self.z /= other
        else:
            raise TypeError("must mul with int or float type")
            
        return self
            
    def __rdiv__(self, other):
        return self / other
    
    def __add__(self, other):
        if isinstance(other, int) or isinstance(other, float):
            self.x += other
            self.y += other
            self.z += other
        elif isinstance(other, Point):
            self.x += other.x
            self.y += other.y
            self.z += other.z
        else:
            raise TypeError("must mul with int or float type")
            
        return self
    
    def __radd__(self, other):
        return self + other
    
    def __sub__(self, other):
        if isinstance(other, int) or isinstance(other, float):
            self.x -= other
            self.y -= other
            self.z -= other
        elif isinstance(other, Point):
            self.x -= other.x
            self.y -= other.y
            self.z -= other.z
        else:
            raise TypeError("must mul with int or float type")
            
        return self
    
    def __rsub__(self, other):
        return self - other
    
    def __str__(self):
        return "(%d, %d, %d)" % (self.x, self.y, self.z)
    
    def __repr__(self):
        return "Point(%d, %d, %d)" % (self.x, self.y, self.z)
    
    @staticmethod
    def One():
        return Point(1, 1, 1)

In [ ]:
point = Point(1, 2, 3)
len(point)

In [ ]:
class Point(object):
    '''This is a Point in 2/3D space.
    You can use .u ro .v for a uv point'''
    
    def __init__(self, x=0, y=0, z=0):
        self.x = x
        self.y = y
        self.z = z
        
    def distanceToPoint(self, point):
        return ((self.x - point.x) + (self.y - point.y) + (self.z - point.z))
        
    def __len__(self):
        return 3
        
    def __getattribute__(self, name):
        if name == 'u':
            return self.x
        elif name == 'v':
            return self.y
            
        return super(Point, self).__getattribute__(name)
    
    def __setattr__(self, name, value):
        if name == 'u':
            self.x = value
        elif name == 'v':
            self.y = value
            
        super(Point, self).__setattr__(name, value)
    
    def __mul__(self, other):
        if isinstance(other, int) or isinstance(other, float):
            self.x *= other
            self.y *= other
            self.z *= other
        else:
            raise TypeError("must mul with int or float type")
            
        return self
            
    def __rmul__(self, other):
        return self * other
    
    def __div__(self, other):
        if isinstance(other, int) or isinstance(other, float):
            self.x /= other
            self.y /= other
            self.z /= other
        else:
            raise TypeError("must mul with int or float type")
            
        return self
            
    def __rdiv__(self, other):
        return self / other
    
    def __add__(self, other):
        if isinstance(other, int) or isinstance(other, float):
            self.x += other
            self.y += other
            self.z += other
        elif isinstance(other, Point):
            self.x += other.x
            self.y += other.y
            self.z += other.z
        else:
            raise TypeError("must mul with int or float type")
            
        return self
    
    def __radd__(self, other):
        return self + other
    
    def __sub__(self, other):
        if isinstance(other, int) or isinstance(other, float):
            self.x -= other
            self.y -= other
            self.z -= other
        elif isinstance(other, Point):
            self.x -= other.x
            self.y -= other.y
            self.z -= other.z
        else:
            raise TypeError("must mul with int or float type")
            
        return self
    
    def __rsub__(self, other):
        return self - other
    
    def __str__(self):
        return "(%d, %d, %d)" % (self.x, self.y, self.z)
    
    def __repr__(self):
        return "Point(%d, %d, %d)" % (self.x, self.y, self.z)
    
    @staticmethod
    def One():
        return Point(1, 1, 1)

In [ ]:
import math

class Vector(Point):
    
    def magnitude(self):
        return math.sqrt((self.x * self.x) + (self.y * self.y) + (self.z * self.z))
    
    def __normalize(self):
        length = self.magnitude()
        x = self.x / length
        y = self.y / length
        z = self.z / length
        
        return (x, y, z)
    
    def normalize(self):
        self.x, self.y, self.z = self.__normalize()
        return self
        
    def normalized(self):
        x, y, z = self.__normalize()
        return Vector(x, y, z)
    
    def __repr__(self):
        return "Vector(%d, %d, %d)" % (self.x, self.y, self.z)

In [ ]:
v1 = Vector(1, 2, 3)
print(v1)
v1

In [ ]:
v1.magnitude()

In [ ]:
v1.normalized()

In [ ]:
import math

class Vector(Point):
    
    def __init__(self, x=0.0, y=0.0, z=.0):
        super(Vector, self).__init__(x, y, z)
        
        self.x = float(x)
        self.y = float(y)
        self.z = float(z)
    
    def magnitude(self):
        return math.sqrt((self.x * self.x) + (self.y * self.y) + (self.z * self.z))
    
    def __normalize(self):
        length = self.magnitude()
        x = self.x / length
        y = self.y / length
        z = self.z / length
        #print(x, y, z)
        return (x, y, z)
    
    def normalize(self):
        self.x, self.y, self.z = self.__normalize()
        return self
        
    def normalized(self):
        x, y, z = self.__normalize()
        return Vector(x, y, z)
    
    def __repr__(self):
        return "Vector(%.3f, %.3f, %.3f)" % (self.x, self.y, self.z)

In [ ]:
v1 = Vector(1, 2, 3)
v2 = v1.normalized()
v2

In [ ]:
v2.x

In [ ]:
print(id(v1), id(v2))

In [ ]:
v1.normalize()

In [ ]:
# --------------------------------------------------------------------------------  
# Copyright (c) 2013 - 2014 Mack Stone. All rights reserved.  
#   
# Permission is hereby granted, free of charge, to any person obtaining a copy  
# of this software and associated documentation files (the "Software"), to deal  
# in the Software without restriction, including without limitation the rights  
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
# copies of the Software, and to permit persons to whom the Software is  
# furnished to do so, subject to the following conditions:  
#   
# The above copyright notice and this permission notice shall be included in  
# all copies or substantial portions of the Software.  
#   
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN  
# THE SOFTWARE.  
# --------------------------------------------------------------------------------