Answers for "from functools import partial"

1

from functools import partial

from functools import partial
  
# A normal function
def f(a, b, c, x):
    return 1000*a + 100*b + 10*c + x
  
# A partial function that calls f with
# a as 3, b as 1 and c as 4.
g = partial(f, 3, 1, 4)
  
# Calling g()
print(g(5))
Posted by: Guest on August-09-2021
1

what does partial do in python

>>> add(1,2)
3
>>> add1 = partial(add, 4)
>>> add1(6)
10
>>> add1(10)
14
# It can basically be used to call classes using a single variable again and again
# For more you can refer to this code
class Conv2dAuto(nn.Conv2d):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.padding =  (self.kernel_size[0] // 2, self.kernel_size[1] // 2) # dynamic add padding based on the kernel_size
        
conv3x3 = partial(Conv2dAuto, kernel_size=3, bias=False)      
conv = conv3x3(in_channels=32, out_channels=64)
# Here conv3x3 can be used again and again without specifying the args and calling the class again and again
Posted by: Guest on December-10-2020

Code answers related to "from functools import partial"

Browse Popular Code Answers by Language