# Class attributes not initialized within \_init\_ method (ROS 5 Days, Topic 8)

**URL:** <https://get-help.theconstruct.ai/t/class-attributes-not-initialized-within-init-method-ros-5-days-topic-8/7482>\
**Category:** ROS Basics In 5 Days (Python)\
**Tags:** class, oop, \_\_init\_\_, python\
**Created:** [January 10, 2021, 4:58am UTC](https://get-help.theconstruct.ai/t/class-attributes-not-initialized-within-init-method-ros-5-days-topic-8/7482 "2021-01-10T04:58:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![izzatfadzlon](https://get-help.theconstruct.ai/letter_avatar_proxy/v4/letter/i/eb8c5e/32.png) [@izzatfadzlon](https://get-help.theconstruct.ai/u/izzatfadzlon)\
**Post date:** [January 10, 2021, 4:58am UTC](https://get-help.theconstruct.ai/t/class-attributes-not-initialized-within-init-method-ros-5-days-topic-8/7482/1 "2021-01-10T04:58:36Z")

</div>

A (not-so)quick question regarding class attributes :

From what I’ve learnt regarding classes(still rather shallow i feel), i always thought it was good programming practice to define any attributes that will be used in the class within the\_init\_ method. However for exercise “8.13”, after a quick Ctrl+F in the code i realized that the attribute \_move\_msg is defined within goal\_callback as a Twist() message type and the methods **above** it are altering the fields within this Twist() instance.

 ![image](https://get-help.robotigniteacademy.com/uploads/default/original/2X/2/20c65c8ebf98aba46cef45f7a8e5ff8547f97cef.png)

 ![image](https://get-help.robotigniteacademy.com/uploads/default/original/2X/6/678256af88771f8f8f9e578fff4029fd2e0e99de.png)

So back to my questions:

1. Why is the \_move\_msg attribute being initially defined in the goal\_callback method instead of the _init_ method?

2. Isn’t an attribute **local** to the method if it isn’t defined within _init_? If so, how is it possible that turn\_drone\_ and move\_forward\_drone methods are calling a local method within the goal\_callback method?

3. This might be similar to question 2, but why isn’t an AttributeError raised if the variable isn’t first defined in the _init_ method?

---

<div class="post-metadata">

**Author:** ![roalgoal](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/roalgoal/32/7280_2.png) [@roalgoal](https://get-help.theconstruct.ai/u/roalgoal)\
**Post date:** [January 11, 2021, 9:09am UTC](https://get-help.theconstruct.ai/t/class-attributes-not-initialized-within-init-method-ros-5-days-topic-8/7482/2 "2021-01-11T09:09:58Z")

</div>

Hi,

You could define it within _init_ just like you said, but if you pay attention to the comments, you’ll see that the attribute `_move_msg` is defined in the callback function that is called when the action server is called (the first line in _init_). That’s why the attribute is defined before any other methods use it.

---

<div class="post-metadata">

**Author:** ![bayodesegun](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/bayodesegun/32/6966_2.png) [@bayodesegun](https://get-help.theconstruct.ai/u/bayodesegun)\
**Post date:** [January 11, 2021, 1:05pm UTC](https://get-help.theconstruct.ai/t/class-attributes-not-initialized-within-init-method-ros-5-days-topic-8/7482/3 "2021-01-11T13:05:11Z")

</div>

1. I agree that the code would be easier to understand if that attribute was defined within ` __init__ `, but it works nevertheless as @roalgoal mentioned.

2. That rule only applies to ordinary Python variables, but this is an instance attribute (or variable) defined with the `self` keyword; it automatically becomes available to all other methods of the class from that point onwards.

3. See point 2 above.

---

<div class="post-metadata">

**Author:** ![izzatfadzlon](https://get-help.theconstruct.ai/letter_avatar_proxy/v4/letter/i/eb8c5e/32.png) [@izzatfadzlon](https://get-help.theconstruct.ai/u/izzatfadzlon)\
**Post date:** [January 14, 2021, 4:30am UTC](https://get-help.theconstruct.ai/t/class-attributes-not-initialized-within-init-method-ros-5-days-topic-8/7482/4 "2021-01-14T04:30:39Z")

</div>

Hi, one last question. After doing a few more exercises, I realized this way of creating publisher objects and messages within the goal\_callback method instead of **init** method is always done in all of them. Is there any particular reason/advantage for us to define the publishers and messages within the goal\_callback method instead of **init**?

---

<div class="post-metadata">

**Author:** ![roalgoal](https://get-help.theconstruct.ai/user_avatar/get-help.theconstruct.ai/roalgoal/32/7280_2.png) [@roalgoal](https://get-help.theconstruct.ai/u/roalgoal)\
**Post date:** [January 14, 2021, 8:42am UTC](https://get-help.theconstruct.ai/t/class-attributes-not-initialized-within-init-method-ros-5-days-topic-8/7482/5 "2021-01-14T08:42:58Z")

</div>

I don’t think there’s a measurable advantage in a simple code like this, rather more of a preference of the coder. In this case, the advantage of defining publishers (and more importantly, subscribers) within a callback is that you make sure that the action server will call on the callback every time it’s executed. This is good practice for things like topics or other continuous information.
