i am trying to set a variable as a function so that it it can be like a menu … basically allow for polymorphism

@jitclass()

class LeverageLong:

leverage_calculator: None

```
def __init__(
self,
leverage_type: int,
):
if leverage_type == 0:
self.leverage_calculator = self.set_static_leverage
elif leverage_type == 1:
self.leverage_calculator = self.calculate_dynamic_leverage
self.leverage_calculator = self.calculate_dynamic_leverage
def set_static_leverage(
self,
):
return 1
def calculate_dynamic_leverage(
self,
):
return 9
```

then i should be able to do something like

lev = LeverageLong(1)

lev.leverage_calculator()

but it is saying the class member isn’t supported … does anyone know how to make something like this work in numba?

I think you’d have to store the selected value then dispatch based on it in a function, like:

```
from numba import njit
from numba.experimental import jitclass
@jitclass()
class LeverageLong:
leverage_type: int
def __init__(self, leverage_type: int):
self.leverage_type = leverage_type
def leverage_calculator(self):
if self.leverage_type == 0:
return self.calculate_static_leverage()
elif self.leverage_type == 1:
return self.calculate_dynamic_leverage()
def calculate_static_leverage(self):
return 1
def calculate_dynamic_leverage(self):
return 9
@njit
def f(leverage_type):
lev = LeverageLong(leverage_type)
return lev.leverage_calculator()
print(f(1))
print(f(0))
```

ok thank you for this … but the problem is that i want to run this through a loop of lets say 10 *example below* … when i do this when i turn numba off it is always checking in the if statement … so the way i would like this to work is that it only checks the if statement one time then instantly executes the dynamic leverage if i had one as the leverage type … do you know how to make it do that ?

@jitclass()

class LeverageLong:

leverage_type: int

```
def __init__(self, leverage_type: int):
self.leverage_type = leverage_type
def leverage_calculator(self):
if self.leverage_type == 0:
return self.calculate_static_leverage()
elif self.leverage_type == 1:
return self.calculate_dynamic_leverage()
def calculate_static_leverage(self):
return 1
def calculate_dynamic_leverage(self):
return 9
```

lev = LeverageLong(1)

for i in range(10):

# print(“hello”)

print(lev.leverage_calculator())

the way this is working is that it goes and does the if statement all 10 times if you don’t have numba on … but i don’t want it to do it that way

here is an example of how this works perfectly … only calls the if statement once

@jitclass()

class LeverageLong:

leverage_calculator: None

```
def __init__(self, leverage_type: int):
if leverage_type == 0:
self.leverage_calculator = self.set_static_leverage
elif leverage_type == 1:
self.leverage_calculator = self.calculate_dynamic_leverage
def set_static_leverage(self):
print(1)
def calculate_dynamic_leverage(self):
print(9)
```

tester = LeverageLong(1)

for i in range(10):

tester.leverage_calculator()