0

I have a class like this:

class Rank (models.Model):
      respect=models.IntegerField(max_length=3)
      #some other attributes

Rank has many attributes (the first of which is respect).

The names of the attributes of Rank are stored in the list, attributes.

I would like to iterate through them, setting them to somevalue, an arbitrary variable.

I'm attempting:

rank=Rank()
for att in attributes:
     rank.att=somevalue #I want rank.(value of att) instead of this
5
  • What function are you trying to send the value of a variable to instead of the variable name? Commented Sep 4, 2013 at 17:42
  • class constructor function if you look at the code you will find out Commented Sep 4, 2013 at 17:44
  • 1
    Your question is kinda misleading. Python uses pass by reference and the only way to pass by value is to use a deep copy of the object (which is terribly inefficient). Also I don't even see any functions in your code besides init. Commented Sep 4, 2013 at 17:44
  • @ShashankGupta No, python does not use pass by reference. If it did, this question would make some sense. Commented Sep 4, 2013 at 17:45
  • He wants rank.att to actually mean rank.{value_of_att} I think. Commented Sep 4, 2013 at 17:45

4 Answers 4

3

I think what you're trying to do is set a value to an attribute by name. This is what the setattr built in method is for.

rank=Rank()
for att in attributes:
     setattr(rank, att, somevalue)
Sign up to request clarification or add additional context in comments.

1 Comment

But site doesn't allow me to set your answer as correct answer
0

You can use the *args or **kwargs wildcard argument. Try the following to see how it works:

def foo(*args, **kwargs):
    print args
    print kwargs

foo(1,2,3)
foo(a=1,b=2,c=3)
foo(1,2,z=3)

Comments

0

You can inject attributes to objects using setattr

for att in attributes:
    setattr(classobj, att.name, att.value)

Note however that this may work or not because the class could have been based on a metaclass that inspects the attributes when creating the class object.

Adding attributes or methods later may work or not, depending on what kind of magic has been used in the metaclass.

If the class wants to inspect the attributes at creation time the only solution (without messing with the class specific magic) is using eval of a string.

Comments

0

In general, you can't pass a name of a variable to a function - python is call by value only*

What you can do, and I think this is what you want, is use getattr and setattr:

for att in attributes:
     print getattr(rank, att)
     setattr(rank, att, somevalue)

* I'm aware of the sect who believe that Python has some other calling convention. It's not a thing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.