Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining function without the brackets?

I understand that my question might sound stupid, and that there might be something in the language definition that explicitly prohibits this notion, but since I don't know about this prohibition, I was wondering whether someone could shed some light on it. In short, I would like to define a python function that I could call from the python shell, but I would like to avoid the brackets. There are cases when a function does not require an argument, and then the bracket only seems to indicate that we are dealing with a function. Such an example would be, if one wants to print the current working directory. I can define a function as

def pwd():
    print os.getcwd()

and then I can call it from the shell as

pwd()

But what if I would like to have a function that I can call as

pwd

Is this possible at all?

like image 483
v923z Avatar asked Oct 08 '11 17:10

v923z


2 Answers

You can't do this without modifying the language or the shell.

If you want to use Python as a shell, you should really try IPython, it allows you to define macros that you can use without typing as many keys. It also lets you do !pwd, you can assign this to a variable as well x = !pwd. It even lets you call single argument functions by writing f x instead of f(x).

BTW Haskell is a language that uses spaces for list of arguments, i.e: f(1,2,3) in Python would be f 1 2 3 in Haskell, and in the shell any IO action can be executed by just typing action.

I forgot there's also a hack you can do:

class Pwd(object):
    def __repr__(self):
        # do pwd command
        # return result in string form
pwd = Pwd()

Now when you type pwd in the shell, it will call __repr__ to get a string representation of the object. Unfortunately, you're restricted to returning a string (as opposed to say, a list of strings representing the files/folders in the current directory, if you were implementing ls) because the python language forces this.

like image 125
megazord Avatar answered Sep 30 '22 07:09

megazord


You're going to get some syntax in there somewhere. You could try something like:

import os
class Shell(object):
    @property
    def pwd(self):
        print os.getcwd()

And then in your interpreter, run:

>>> s = Shell()
>>> s.pwd
/tmp
like image 28
Kirk Strauser Avatar answered Sep 30 '22 08:09

Kirk Strauser