python - Defining function without the brackets? -
i understand question might sound stupid, , there might in language definition explicitly prohibits notion, since don't know prohibition, wondering whether shed light on it. in short, define python function call python shell, avoid brackets. there cases when function not require argument, , bracket seems indicate dealing function. such example be, if 1 wants print current working directory. can define function as
def pwd(): print os.getcwd()
and can call shell
pwd()
but if have function can call
pwd
is possible @ all?
you can't without modifying language or shell.
if want use python shell, should try ipython, allows define macros can use without typing many keys. lets !pwd
, can assign variable x = !pwd
. lets call single argument functions writing f x
instead of f(x)
.
btw haskell language uses spaces list of arguments, i.e: f(1,2,3)
in python f 1 2 3
in haskell, , in shell io action can executed typing action
.
i forgot there's hack can do:
class pwd(object): def __repr__(self): # pwd command # return result in string form pwd = pwd()
now when type pwd in shell, call __repr__
string representation of object. unfortunately, you're restricted returning string (as opposed say, list of strings representing files/folders in current directory, if implementing ls) because python language forces this.
Comments
Post a Comment