Name Mangling

  • technika do tworzenia lokalnej kopii w scopie klasy
  • __nazwa -> _classname__nazwa
  • pozwala na uniknięcie nadpisania metody w klasach dziedziczących

In [ ]:
class Point():
    """This is a docstring"""
    
    version = "0.1"
    
    def __init__(self, x, y, version=None):
        self.x = x
        self.y = y
        if version is not None:
            self.version = version
            
    def quotient(self):
        p = self.x * self.y
        return 10 * p
        
    def double(self):
        return self.x * self.y
    
    def print_version(self):
        print(self.version)
        
p = Point(1, 2)
print(p.quotient())

In [ ]:
class Point():
    """This is a docstring"""
    
    version = "0.1"
    
    def __init__(self, x, y, version=None):
        self.x = x
        self.y = y
        if version is not None:
            self.version = version
            
    def quotient(self):
        p = self.double()
        return 10 * p
        
    def double(self):
        return self.x * self.y
    
    def print_version(self):
        print(self.version)

p = Point(1, 2)
print(p.quotient())

In [ ]:
class Point3D(Point):
    
    def __init__(self, x, y, z, version=None):
        self.z = z
        super().__init__(x, y, version)
        
    def double(self):
        return self.z * self.x * self.y
        
        
p = Point3D(1, 2, 3, "1")
print(p.quotient())

In [ ]:
class Point():
    """This is a docstring"""
    
    version = "0.1"
    
    def __init__(self, x, y, version=None):
        self.x = x
        self.y = y
        if version is not None:
            self.version = version
            
    def quotient(self):
        p = self.__double()
        return 10 * p
        
    def double(self):
        return self.x * self.y
    
    def print_version(self):
        print(self.version)
        
    __double = double

print(Point(1, 2).quotient())
#print(Point(1, 2).__double)
#print(Point.__double)
#print(Point._Point__double)

In [ ]:
class Point3D(Point):
    
    def __init__(self, x, y, z, version=None):
        self._z = z
        super().__init__(x, y, version)
        
    def double(self):
        return self._z * self.x * self.y
        
        
p = Point3D(1, 2, 3, "1")
print(p.quotient())