Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python interpreters and Alias's

Tags:

python

Is there any mechanism similar to alias's (with something like BASH) that can be used in Ipython or the Python Interpreter?

For instance if I find myself frequently doing something like:

var = urllib2.urlopen('http://programmers.stackexchange.com')

But I don't want to continually type out those strings.

Is there any method of (Persistently between exits) shortening the request other than writing a script for it?

like image 913
Jay Holister Avatar asked Jan 31 '26 11:01

Jay Holister


1 Answers

No, but in your interpreter, write this:

def pse_url():
    global var
    var = urllib2.urlopen('http://programmers.stackexchange.com')

Then, write pse_url() whenever you need to affect your variable. It would be cleaner to not use a global variable:

var = pse_url()

If you have many such utilities, put them in your own module and load them once when you start the REPL.

like image 192
coredump Avatar answered Feb 03 '26 08:02

coredump



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!