Feb-17-2024, 11:31 AM
I am just looking at Mandelbrot fractals. This class comes from realpython.com I know next to nothing about classes.
I saved this class as a module in my myModules folder and import the class MandelbrotSet :
from mandelbrotV2 import MandelbrotSet
Why does __contains__() use double underscore? I read the double underscore keeps the function hidden??
I saved this class as a module in my myModules folder and import the class MandelbrotSet :
from mandelbrotV2 import MandelbrotSet
Why does __contains__() use double underscore? I read the double underscore keeps the function hidden??
# mandelbrotV2.py from dataclasses import dataclass from math import log @dataclass class MandelbrotSet: max_iterations: int escape_radius: float = 2.0 def __contains__(self, c: complex) -> bool: return self.stability(c) == 1 def stability(self, c: complex, smooth=False, clamp=True) -> float: value = self.escape_count(c, smooth) / self.max_iterations return max(0.0, min(value, 1.0)) if clamp else value def escape_count(self, c: complex, smooth=False) -> int | float: z = 0 for iteration in range(self.max_iterations): z = z ** 2 + c if abs(z) > self.escape_radius: if smooth: return iteration + 1 - log(log(abs(z))) / log(2) return iteration return self.max_iterations