Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 894813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:27:28+00:00 2026-05-15T14:27:28+00:00

I am calling a python script with the following command line: myscript.py –myopt=[(5.,5.),(-5.,-5.)] The

  • 0

I am calling a python script with the following command line:

myscript.py --myopt="[(5.,5.),(-5.,-5.)]"

The question is — how to convert myopt to a list variable. My solution was to use optparse, treating myopt as a string, and using

(options, args) = parser.parse_args()    
myopt = eval(options.myopt)

Now, because I used eval() I feel a bit like Dobby the house elf, having knowingly transgressed the commandments of great (coding) wizards, and wanting to self-flagellate myself in punishment.

But are there better options for parsing lists or tuples or lists of tuples from the command line?.. I’ve seen solutions that use split(), but this won’t work here since this isn’t a simple list. Keep in mind, also, that this is being done in the context of mostly one-off scientific computing with no security concerns — so perhaps eval() isn’t as evil here?..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:27:28+00:00Added an answer on May 15, 2026 at 2:27 pm

    ast.literal_eval(node_or_string):

    Safely evaluate an expression node or
    a string containing a Python
    expression. The string or node
    provided may only consist of the
    following Python literal structures:
    strings, numbers, tuples, lists,
    dicts, booleans, and None.

    This can be used for safely evaluating
    strings containing Python expressions
    from untrusted sources without the
    need to parse the values oneself.

    So you can do

    import ast
    (options, args) = parser.parse_args()    
    myopt = ast.literal_eval(options.myopt)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 510k
  • Answers 510k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Starting with your code: int[] ar1=ar.Take(k-1).Reverse().ToArray(); int[] ar2=ar.Skip(k - 1).Take(ar.Length… May 16, 2026 at 4:53 pm
  • Editorial Team
    Editorial Team added an answer Realizing that the Sun's JARs are not on Maven central… May 16, 2026 at 4:53 pm
  • Editorial Team
    Editorial Team added an answer What are you using on the client side? A web… May 16, 2026 at 4:53 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Is there a simple way to run a Python script on Windows/Linux/OS X? On
I have a Python project that has the following structure: package1 class.py class2.py ...
I have the following python 3 file: import base64 import xxx str = xxx.GetString()
I wrote a python script that rotates an image 90 degrees. I am including
In GAE (Python), using the webApp Framework, calling self.redirect('some_url') redirects the user to that
Is it possible to encapsulate python modules 'mechanize' and 'BeautifulSoup' into a single .py
Over time I found the need to override several stdlib methods from Python in
I'm just starting getting into Python - my focus being on using it with
okay code: #!/usr/bin/python import wx import sys class XPinst(wx.App): def __init__(self, redirect=False, filename=None): wx.App.__init__(self,
Update: based on Lee's comment I decided to condense my code to a really

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.