Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call external function without sending 'self' arg

Tags:

python

I'm writing tests for a Django application and using a attribute on my test class to store which view it's supposed to be testing, like this:

# IN TESTS.PY
class OrderTests(TestCase, ShopTest):
    _VIEW = views.order

    def test_gateway_answer(self):
        url = 'whatever url'
        request = self.request_factory(url, 'GET')
        self._VIEW(request, **{'sku': order.sku})


# IN VIEWS.PY
def order(request, sku)
    ...

My guess is that the problem I'm having is caused because since I'm calling an attribute of the OrderTests class, python assumes I wanna send self and then order get the wrong arguments. Easy to solve... just not use it as a class attribute, but I was wondering if there's a way to tell python to not send self in this case.

Thanks.

like image 439
Gabe Avatar asked Apr 16 '13 14:04

Gabe


People also ask

Can we call a function without passing arguments?

to answer your question concerning calling functions without their parameters. it's possible for functions where default values for all parameters are set e.g.: def foo(bar = "default string"): print(bar) print(foo()) # prints: default string print(foo("hello world!")) # prints: hello world!

How do you call a function without self in Python?

Python also accepts function recursion, which means a defined function can call itself. Recursion is a common mathematical and programming concept. It means that a function calls itself. This has the benefit of meaning that you can loop through data to reach a result.

Do I need to pass self as an argument Python?

Need for Self in PythonPython uses the self parameter to refer to instance attributes and methods of the class. Unlike other programming languages, Python does not use the “@” syntax to access the instance attributes. This is the sole reason why you need to use the self variable in Python.

Why do we pass self in Python method?

The self is used to represent the instance of the class. With this keyword, you can access the attributes and methods of the class in python. It binds the attributes with the given arguments. The reason why we use self is that Python does not use the '@' syntax to refer to instance attributes.


1 Answers

This happens because in Python functions are descriptors, so when they are accessed on class instances they bind their first (assumed self) parameter to the instance.

You could access _VIEW on the class, not on the instance:

class OrderTests(TestCase, ShopTest):
    _VIEW = views.order

    def test_gateway_answer(self):
        url = 'whatever url'
        request = self.request_factory(url, 'GET')
        OrderTests._VIEW(request, **{'sku': order.sku})

Alternatively, you can wrap it in staticmethod to prevent it being bound to the instance:

class OrderTests(TestCase, ShopTest):
    _VIEW = staticmethod(views.order)

    def test_gateway_answer(self):
        url = 'whatever url'
        request = self.request_factory(url, 'GET')
        self._VIEW(request, **{'sku': order.sku})
like image 68
ecatmur Avatar answered Nov 24 '22 06:11

ecatmur