Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I shorten this line of Python code?

Here is the line that needs to be shortened.

tree_top = os.path.abspath(os.path.expanduser(os.path.expandvars(sys.argv[1])))
  • Should I create a variable for each procedure?
  • Should I alias os.path,abspath, os.path.expandvars and os.path.expanduser to have shorter names?
  • Should I use backslashes?
like image 353
Ricky Wilson Avatar asked Oct 20 '25 03:10

Ricky Wilson


1 Answers

The easiest way to reduce the width is to use implicit line continuation within parentheses:

tree_top = os.path.abspath(
    os.path.expanduser(
        os.path.expandvars(sys.argv[1])
    )
)

Alternatively, just select the parts of os.path that you need:

from os.path import abspath, expanduser, expandvars

tree_top = abspath(expanduser(expandvars(sys.argv[1])))

or use some combination of the two.

like image 156
jonrsharpe Avatar answered Oct 22 '25 18:10

jonrsharpe



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!