How to use a python keyword as a method name in controller

27 views
Skip to first unread message

Keshava Bharadwaj

unread,
Apr 14, 2014, 6:37:30 AM4/14/14
to peca...@googlegroups.com
Hi,

I have a requirement where i have a python keyword 'import' in my rest url.
This has to be 'import'


now, i cannot create a method named import in my controller and expose it. i.e the following is not possible.
since import is a keyword, python does not allow you to declare this.

class TestController():
    def import():
        pass


Is there a way to achieve this ?

Jonathan LaCour

unread,
Apr 14, 2014, 3:48:02 PM4/14/14
to peca...@googlegroups.com, Keshava Bharadwaj
> I have a requirement where i have a python keyword 'import' in my rest url.
> This has to be 'import'
>
> Eg : GET http://localhost:1234/links/import

> [snip, snip]
>
> Is there a way to achieve this ?

Yes. Simply alias the method on the controller, as follows:

class MyController(object):
    @expose()
    def _import(self):
        return ‘Hello, World!’

setattr(MyController, ‘import’, MyController._import)

That should do the trick!

--
Jonathan LaCour
Reply all
Reply to author
Forward
0 new messages