When the class calls itself like that it’s the __call__ method in the class that it is calling, like operator(). __init__ is like a constructor and is called when the class is instantiated, so everything in init is available by the time the class gets to __call__.
class ReducedMomentum:
# here is where an instance of the object calls itself
def __call__(self, kinetic_energy):
"""Example
reduced_momentum = ReducedMomentum(5)
reduction = reduced_momentum(5)
"""
return math.sqrt(kinetic_energy * (kinetic_energy + 2 * self.mass)) / self.mass
So for your Ktilde class, for example
class Ktilde:
def __init__(self, alpha_1, tritium_mass, electron_mass_eV):
"""Constructor needing above arguments
alpha_1: float?
tritium_mass: float?
electron_mass_eV: float?
Example:
k_tilde = Ktilde(1., 1., 1.) # initialize with params
some_spectator_mass = 2. # some other relevant numbers
some_electron_KE = 302.1
calculation = k_tilde(some_spectator_mass, some_electron_KE) # uses the __call__ method in this class
"""
self.alpha_1 = alpha_1
self.mass_fraction = MassFraction(tritium_mass, electron_mass_eV / ATOMIC_MASS)
self.reduced_momentum = ReducedMomentum(electron_mass_eV)
def __call__(self, spectator_mass, electron_kinetic_energy):
return self.alpha_1 * self.mass_fraction(spectator_mass) * self.reduced_momentum(electron_kinetic_energy)
So this does
ktilde = Ktilde(alpha_1, spectator_masses, electron_mass) # ktilde instance
mass_spectator = parse_spectator_mass(args, parser) # some function to get mass of spectator
ktilde_value = ktilde(mass_spectator, args.energy) # uses __call__ method of KTilde, args is some object (maybe from argparser) that has an energy attribute
print('{:.4f}'.format(ktilde_value))
6
solved Converting Python Script to C++ [closed]