# Function to set component property

**URL:** <https://forum.inductiveautomation.com/t/function-to-set-component-property/15074>\
**Category:** Ignition\
**Created:** [August 15, 2017, 7:51pm UTC](https://forum.inductiveautomation.com/t/function-to-set-component-property/15074 "2017-08-15T19:51:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamthebull](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@iamthebull](https://forum.inductiveautomation.com/u/iamthebull)\
**Post date:** [August 15, 2017, 7:51pm UTC](https://forum.inductiveautomation.com/t/function-to-set-component-property/15074/1 "2017-08-15T19:51:46Z")

</div>

I want to write a python function that will set the value of a given property on the component. For example:

```auto
def setPropertyValue(component, propertyName, value):
	component.propertyName = value

```

I have accomplished this using the `exec()` function:

```auto
def setPropertyValue(component, propertyName, value):
	exec('component.%s = value' % propertyName)

```

Is there any other way of doing this? Is there a method for components that I can use to set the value of a given property?

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [August 15, 2017, 8:48pm UTC](https://forum.inductiveautomation.com/t/function-to-set-component-property/15074/2 "2017-08-15T20:48:00Z")

</div>

This works:

```auto
def setPropertyValue(component,propertyName,value):
	method = getattr(component,"set%s"% propertyName.title())	
 	method(value)

```

In this function we get the set method of the property and then call it to set the value.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 15, 2017, 10:59pm UTC](https://forum.inductiveautomation.com/t/function-to-set-component-property/15074/3 "2017-08-15T22:59:24Z")

</div>

Why wouldn’t one just use python’s `setattr()` function? Like so:

```auto
def setPropertyValue(comp, prop, val):
    setattr(comp, prop, val)

```

Consider catching AttributeError, though, to fall back on the setPropertyValue() method applicable to custom properties (not always supported by setattr). Like so:

```auto
def setPropertyValue(comp, prop, val):
    try:
        setattr(comp, prop, val)
    except AttributeError:
        return comp.setPropertyValue(prop, val)

```

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [August 15, 2017, 11:16pm UTC](https://forum.inductiveautomation.com/t/function-to-set-component-property/15074/4 "2017-08-15T23:16:36Z")

</div>

Yes, I like setattr better.

---

<div class="post-metadata">

**Author:** ![iamthebull](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@iamthebull](https://forum.inductiveautomation.com/u/iamthebull)\
**Post date:** [August 15, 2017, 11:32pm UTC](https://forum.inductiveautomation.com/t/function-to-set-component-property/15074/5 "2017-08-15T23:32:13Z")

</div>

Me too! I was just fighting with this because it was failing with custom properties. Thanks!

---

<div class="post-metadata">

**Author:** ![iamthebull](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@iamthebull](https://forum.inductiveautomation.com/u/iamthebull)\
**Post date:** [September 8, 2017, 3:24pm UTC](https://forum.inductiveautomation.com/t/function-to-set-component-property/15074/6 "2017-09-08T15:24:57Z")

</div>

> [@pturmel](#):
>
> custom properties (not always supported by setattr)

In what situation will `setattr()` not work on custom properties?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [September 8, 2017, 4:30pm UTC](https://forum.inductiveautomation.com/t/function-to-set-component-property/15074/7 "2017-09-08T16:30:51Z")

</div>

> [@iamthebull](#):
>
> In what situation will setattr() not work on custom properties?

When the component is retrieved through Java Swing methods and lists that don't know about IA's custom python wrapping, or were python-wrapped before the custom type converter was registered with Py.java2py(). I haven't exhaustively determined where this applies, so I tend to use the error-checking wrapper everywhere I mix custom and built-in properties. I try to remember to use the methods whenever the relevant code is always accessing a custom property.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 4, 2018, 12:31pm UTC](https://forum.inductiveautomation.com/t/function-to-set-component-property/15074/9 "2018-04-04T12:31:40Z")

</div>

> [@pturmel](#):
>
> or were python-wrapped before the custom type converter was registered with Py.java2py()

If you visit [this thread](http://forum.inductiveautomation.com/t/transaction-groups-triggered-expression-item-updating-when-trigger-is-false/17196/13) you'll see that Ignition was not registering its wrapper at all. If you install the release candidate of Simulation Aids posted at the end of that thread, you won't have to use the getPropertyValue/setPropertyValue methods any more. The python property substitution for custom properties will "just work" in all cases.
