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 8437045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:19:47+00:00 2026-06-10T07:19:47+00:00

Question I want to accomplish the following: tmp = do_stuff(tmp2) if tmp != None:

  • 0

Question

I want to accomplish the following:

tmp = do_stuff(tmp2)
if tmp != None:
    num = tmp

However the variables to be assigned in my problem are variables (better: properties) of an object instance:

class mycls(object):
    def __init__(self):
        self.__a, self._b, self.c = 2, 3, 5
    def set_a(self, val): self.__a = val
    def set_b(self, val): self._b = val
    def set_c(self, val): self.c = val
    A = property(fset = set_a)
    B = property(fset = set_b)
    C = property(fset = set_c)
tmp2 = [7, 11, 13]
inst = mycls()
tmp = do_stuff(tmp2[0])
if tmp != None: inst.A = tmp
tmp = do_stuff(tmp2[1])
if tmp != None: inst.B = tmp
tmp = do_stuff(tmp2[2])
if tmp != None: inst.C = tmp

Obviously the last part looks highly repetitive and in my case needs to be applied to over 10 variables. My question now is: how can those last 6 lines be compressed?

Own attempts at solving the problem

Ideally I would prefer a solution like:

for i in [[inst.A, 0], [inst.B, 1], [inst.C, 2]]:
    tmp = do_stuff(tmp2[i[1]])
    if tmp != None: i[0] = tmp

However this doesn’t work (because the inst.X are evaluated and you can’t store primitive datatypes by reference/pointer). Another approach I came up with was to use the variable names as strings and fiddle with inst.__dict__, e.g. like this:

for i in [["A", 0], ["B", 1], ["C", 2]]:
    tmp = do_stuff(tmp2[i[1]])
    if tmp != None: inst.__dict__[i[0]] = tmp

However this plan was foiled by the fact that A, B and C are properties.

LISP-like macros would be an enormous help here, unfortunately Python doesn’t seem to support macros and I don’t want to add dependencies to one of the macro libraries for Python I found on-line (which I didn’t test since I won’t be able to use them anyway).

I also want to avoid using anonymous functions in a list since this will likely result in the same amount of code as in the second code listing.

My last resort to solve this problem would currently be evals, which I wanted to avoid at all cost.

  • 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-06-10T07:19:48+00:00Added an answer on June 10, 2026 at 7:19 am

    You are looking for setattr():

    for i in [["A", 0], ["B", 1], ["C", 2]]:
        tmp = do_stuff(tmp2[i[1]])
        if tmp != None: setattr(inst, i[0], tmp)
    

    To quote the documentation:

    This is the counterpart of getattr(). The arguments are an object, a string and an arbitrary value. The string may name an existing attribute or a new attribute. The function assigns the value to the attribute, provided the object allows it. For example, setattr(x, 'foobar', 123) is equivalent to x.foobar = 123.

    Note that since None is a singleton in python, usually one tests for it using the is or is not operators:

    for i in [["A", 0], ["B", 1], ["C", 2]]:
        tmp = do_stuff(tmp2[i[1]])
        if tmp is not None:
            setattr(inst, i[0], tmp)
    

    and the PEP 8 python styleguide discourages compound statements; keep the if suite on it’s own line, even if it is just one line.

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

Sidebar

Related Questions

I want to accomplish nearly the same thing as this question , which is
This question does pretty much what I want to accomplish, but my table is
EDIT: Simple version of the question: I want to create server variables in the
That is the question. What I want to accomplish is something similar to the
Question: I want to add a unique constraint on a mapping table (n:n). I
Question: I want code for: syntax highlighting (of programming languages) Language: C# or assembly
Similar Stack Overflow Question I want users to be able to search through my
I have a little question I want to click on my smileys and insert
There are actually 2 question i want to cover in this topic. 1) Is
I have a interesting question: I want to split the year into 4 quarters.

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.