# A small question in python

**URL:** <https://forum.ubiquityrobotics.com/t/a-small-question-in-python/828>\
**Category:** Uncategorized\
**Created:** [June 11, 2021, 8:02pm UTC](https://forum.ubiquityrobotics.com/t/a-small-question-in-python/828 "2021-06-11T20:02:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![IngeniousDoc](https://forum.ubiquityrobotics.com/letter_avatar_proxy/v4/letter/i/94ad74/32.png) [@IngeniousDoc](https://forum.ubiquityrobotics.com/u/IngeniousDoc)\
**Post date:** [June 11, 2021, 8:02pm UTC](https://forum.ubiquityrobotics.com/t/a-small-question-in-python/828/1 "2021-06-11T20:02:40Z")

</div>

So, I am trying to add the sonar range values to my code. I added the call back function in the same class. My question is, is the method in which I defined it means it’s accessible within the other functions?

 ![image](https://frm.ubiquityrobotics.com/uploads/default/original/1X/e55b781b44b6651dc8e1b58aaa2e8dec6f4aa76d.png)

So, I added a subscriber, and finally the call back function. and I was just wondering if the range value would be accessible in the camera callback function?

Based on my understanding, it should be. Just wanted to confirm

---

<div class="post-metadata">

**Author:** ![mjstn2011](https://forum.ubiquityrobotics.com/user_avatar/forum.ubiquityrobotics.com/mjstn2011/32/30_2.png) [@mjstn2011](https://forum.ubiquityrobotics.com/u/mjstn2011)\
**Post date:** [June 12, 2021, 2:37am UTC](https://forum.ubiquityrobotics.com/t/a-small-question-in-python/828/2 "2021-06-12T02:37:55Z")

</div>

I try to setup class variables in the init function which also sets their type (a common python PITA).

So in your init you could use  
self.sonarId = 0  
self.sonarRange = 9.9

(I like to set sonar ranges to some fixed value and not zero so you can see when they change from 0 or not)

I would avoid initial definition of a class member in a callback and prefer setting them up right from the start in init.

You are welcome to mess with python scope experiments and print self.sonarRange from time to time in your main which would answer your question as well.

Disclaimer: I am no python expert by any means but use it because it is very powerful and easy to change on the fly. As you know there are a ba-zillion python info pages on the web.
