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

  • SEARCH
  • Home
  • 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 156881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:24:17+00:00 2026-05-11T10:24:17+00:00

My eval syntax isn’t right. Namely, for each category, I’d like to output a

  • 0

My eval syntax isn’t right. Namely, for each category, I’d like to output a ModelChoiceField named category_task, ie. if category were ‘fun’, then a radio select field ‘fun_tasks’ would be output.

categories = Category.objects.all()  for category in categories:     eval(category)_tasks = form.ModelChoiceField(         queryset         = Task.objects.filter(method__category=category),         widget           = RadioSelect     ) 
  • 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. 2026-05-11T10:24:18+00:00Added an answer on May 11, 2026 at 10:24 am

    “eval is evil.”

    OK, it has its uses, but 90% of eval usage (in any language) is misconceived, so if you find yourself writing an eval you should stop and examine what you’re doing with extreme suspicion.

    eval(category)_tasks = x 

    If you are doing an assignment, that’s a statement rather than an expression, so you’d have to use exec rather than eval:

    exec category+'_tasks= x' 

    However exec is just as evil as eval!

    You can write a variable in Python without having to parse/evaluate Python code:

    locals()[category+'_tasks']= x 

    or, if you want to write a global variable instead of one in the current scope, replace locals() with globals().

    Although this is better than eval/exec, it is still rather code-smelly. You rarely actually want completely dynamically-named variables; a lookup is usually much cleaner:

    catlookup= {} catlookup[category]= x 

    although without more context it’s difficult to say what’s best for your case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why isn't this working? eval $response = $ua->request($r); print $@; gives: syntax error at
I'm trying to figure out how to rescue syntax errors that come up when
I would like to be able to use ediff with git mergetool. I found
Is this syntax functionally equivalent def self.included(base) base.class_eval do extend ClassMethods end end to
I thought I would ask this question to see why many examples and people
This should be really easy but I can't figure out how to make it
In my markup I am using HTML title attributes which I set by the
I found a simple pure python blowfish implementation that meets my needs for a
I'm rather new to Ruby, and so far, figuring out how to use binding

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.