# How to access methods from object after it is saved in a variable

**URL:** <https://forum.robotframework.org/t/how-to-access-methods-from-object-after-it-is-saved-in-a-variable/5411>\
**Category:** Robot Framework\
**Created:** [8 March 2023 04:59 UTC](https://forum.robotframework.org/t/how-to-access-methods-from-object-after-it-is-saved-in-a-variable/5411 "2023-03-08T04:59:42Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![adathatri](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/adathatri/32/2417_2.png) [@adathatri](https://forum.robotframework.org/u/adathatri)\
**Post date:** [8 March 2023 04:59 UTC](https://forum.robotframework.org/t/how-to-access-methods-from-object-after-it-is-saved-in-a-variable/5411/1 "2023-03-08T04:59:42Z")

</div>

I have created a Python library with a method that returns the object self. I saved this object into a robot variable. But I am unable to access the method present in this object by using syntax `${var}.method_name` or `${var.name}`

Python library:

```python
class sing():
    def __init__ (self):
        pass
    def init(self):
        return self
    def name(self):
        return "hi"

```

Robot code:

```python
Test
   ${val}= Init
   Log To Console \n${val}
   ${word}= Run Keyword ${val}.name
   Log To Console ${word}

```

Result:

```python
==============================================================================
Test .
<sing.sing object at 0x7ffff06584e0>
Test | FAIL |
No keyword with name '<sing.sing object at 0x7ffff06584e0>.name' found.
------------------------------------------------------------------------------

```

Trying to access as ${var.name}

```python
Test
   ${val}= Init
   Log To Console \n${val}
   ${word}= Run Keyword ${val.name}
   Log To Console ${word}

```

Result:

```python
Test .
<sing.sing object at 0x7ffff0658d30>
Test | FAIL |
Keyword name must be a string.

```

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [8 March 2023 07:32 UTC](https://forum.robotframework.org/t/how-to-access-methods-from-object-after-it-is-saved-in-a-variable/5411/2 "2023-03-08T07:32:53Z")

</div>

Hi Anusha,

You were nearly there, change this line

```python
   ${word}= Run Keyword ${val.name}

```

to

```python
   ${word}= Set Variable ${val.name()}

```

Dave.

FYI - here’s a full working example:

_sing.py_

```python
class sing():
    def __init__ (self):
        pass
    def init(self):
        return self
    def name(self):
        return "hi"

```

_Anusha.robot_

```python
***Settings***
Library sing.py

***Test Cases***
Test
   ${val}= Init
   Log To Console \n${val}
   ${word}= Set Variable ${val.name()}
   Log To Console ${word}

```

Console Log:

```python
$ robot Anusha.robot 
==============================================================================
Anusha                                                                        
==============================================================================
Test .
<sing.sing object at 0x7f887b0e5000>
..hi
Test | PASS |
------------------------------------------------------------------------------
Anusha | PASS |
1 test, 1 passed, 0 failed
==============================================================================
Output: /home/dave/Documents/tmp/Anusha/output.xml
Log: /home/dave/Documents/tmp/Anusha/log.html
Report: /home/dave/Documents/tmp/Anusha/report.html

```

HTML Log:

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/robotframework1/original/2X/3/38f2d6d657c2f1b8cd212372a071c14a8a81da79.png)

---

<div class="post-metadata">

**Author:** ![adathatri](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/adathatri/32/2417_2.png) [@adathatri](https://forum.robotframework.org/u/adathatri)\
**Post date:** [8 March 2023 08:39 UTC](https://forum.robotframework.org/t/how-to-access-methods-from-object-after-it-is-saved-in-a-variable/5411/3 "2023-03-08T08:39:35Z")

</div>

Works like a charm… Thank you so much…

---

<div class="post-metadata">

**Author:** ![pekkaklarck](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/pekkaklarck/32/1019_2.png) [@pekkaklarck](https://forum.robotframework.org/u/pekkaklarck)\
**Post date:** [8 March 2023 15:36 UTC](https://forum.robotframework.org/t/how-to-access-methods-from-object-after-it-is-saved-in-a-variable/5411/4 "2023-03-08T15:36:37Z")

</div>

Notice that simply using

```python
***Settings***
Library sing.py

***Test Cases***
Test
   ${word}= Name
   Log To Console ${word}

```

ought to work the same way. Unless you have some specific reason for your approach, this is a lot simpler.

---

<div class="post-metadata">

**Author:** ![adathatri](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/adathatri/32/2417_2.png) [@adathatri](https://forum.robotframework.org/u/adathatri)\
**Post date:** [24 March 2023 12:59 UTC](https://forum.robotframework.org/t/how-to-access-methods-from-object-after-it-is-saved-in-a-variable/5411/5 "2023-03-24T12:59:47Z")

</div>

I don’t get the same behaviour when I execute the below code. I get Keyword name cannot be empty error. I basically want to execute any method from the obj stored in variable val.

```
***Settings***
Library sing.py

***Test Cases***
Test
   ${val}= Init
   Log To Console \n${val}
   ${word}= ${val.name()}
   Log To Console ${word}

```

---

<div class="post-metadata">

**Author:** ![adathatri](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/adathatri/32/2417_2.png) [@adathatri](https://forum.robotframework.org/u/adathatri)\
**Post date:** [24 March 2023 13:01 UTC](https://forum.robotframework.org/t/how-to-access-methods-from-object-after-it-is-saved-in-a-variable/5411/6 "2023-03-24T13:01:30Z")

</div>

Thanks… I have a python file that returns more than 1 object at a time. So I’m finding ways to save the returned object into a variable in robot and then call the methods in that object.

---

<div class="post-metadata">

**Author:** ![adathatri](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/adathatri/32/2417_2.png) [@adathatri](https://forum.robotframework.org/u/adathatri)\
**Post date:** [27 March 2023 14:55 UTC](https://forum.robotframework.org/t/how-to-access-methods-from-object-after-it-is-saved-in-a-variable/5411/7 "2023-03-27T14:55:22Z")

</div>

Can someone please help me with this code?

```
***Settings***

```

Library sing.py

\*\*\* Test Cases \*\*\*  
Test  
${val}= Init  
Log To Console \n${val}  
${word}= ${val.name()}  
Log To Console ${word}

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [28 March 2023 04:14 UTC](https://forum.robotframework.org/t/how-to-access-methods-from-object-after-it-is-saved-in-a-variable/5411/8 "2023-03-28T04:14:49Z")

</div>

Hi Anusha,

It’s usually a good idea to show all the relevant code as well as any error messages when asking for help, but I’ll try.

Q1. is sing.py still the same?  
Q2. does the name() function in sing.py still return “`hi`”?

Q3. Assuming the answer to Q2 is yes, then do you have a keyword called hi somewhere ?

If the answer to Q3 is No, then your problem is probably in this line:

```python
${word}= ${val.name()}

```

And you probably got a message something about not being able to find a keyword called “Hi”

That’s my best guess,

Dave.

---

<div class="post-metadata">

**Author:** ![adathatri](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/adathatri/32/2417_2.png) [@adathatri](https://forum.robotframework.org/u/adathatri)\
**Post date:** [28 March 2023 05:31 UTC](https://forum.robotframework.org/t/how-to-access-methods-from-object-after-it-is-saved-in-a-variable/5411/9 "2023-03-28T05:31:22Z")

</div>

Here is my code:

cat sing.py  
#!/usr/bin/env python

```
class sing():
def __init__ (self):
    pass
def init(self):
    return self
def name(self):
    return "hi"

```

cat anusha.robot

```
***Settings***
Library sing.py

***Test Cases***
Test
   ${val}= Init
   Log To Console \n${val}
   ${word}= ${val.name()}
   Log To Console ${word}

```

So I am trying to execute method - `name` from the object ${val} so that the variable ${word} gets the value “hi”.

The error that I see when I execute anusha.robot is “Keyword name cannot be empty.”

```
bash-4.2$ python -m robot anusha.robot 
==============================================================================
Anusha                                                                        
==============================================================================
Test .
<sing.sing object at 0x7ffff0658cc0>
Test | FAIL |
Keyword name cannot be empty.
------------------------------------------------------------------------------

```

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [28 March 2023 05:47 UTC](https://forum.robotframework.org/t/how-to-access-methods-from-object-after-it-is-saved-in-a-variable/5411/10 "2023-03-28T05:47:16Z")

</div>

Hi Anusha,

> [@damies13](#):
>
> `${word}= Set Variable ${val.name()}`

> [@adathatri](#):
>
> `${word}= ${val.name()}`

`Keyword name cannot be empty.` Ah ok, I guess that makes sense, because the keyword hi doesn’t exist there’s no value to assign to to `${word}`, In my example from a while back I used the keyword `Set Variable` to set the value of `${word}` to hi.

Robot Framework evaluates `${val.name()}` first and then tries to process the line with robot interpreter so this:

```python
${word}= ${val.name()}

```

is the same to robot framework as this:

```python
${word}= hi

```

The python function call (`val.name()`) is processed first → it’s value (hi) is passed into the variable `${val.name()}` → then robot framework tries to work out what to do with `${word}= hi`, and obviously this is not valid.

The question is what were you wanting it to do? what was the reason to remove `Set Variable`?

Dave.

---

<div class="post-metadata">

**Author:** ![adathatri](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/adathatri/32/2417_2.png) [@adathatri](https://forum.robotframework.org/u/adathatri)\
**Post date:** [28 March 2023 06:44 UTC](https://forum.robotframework.org/t/how-to-access-methods-from-object-after-it-is-saved-in-a-variable/5411/11 "2023-03-28T06:44:51Z")

</div>

My primary intention is to be able to run the object methods. The methods themselves may or may not return any value. . For example like this:

```
bash-4.2$ cat sing.py 
#!/usr/bin/env python

class sing():
    def __init__ (self):
        pass
    def init(self):
        return self
    def name(self):
        print("hi")
bash-4.2$ cat anusha.robot 
***Settings***
Library sing.py

***Test Cases***
Test
   ${val}= Init
   Log To Console \n${val}
   ${val.name()}

```

But I get the same error `Keyword name cannot be empty.`  
I understand why this behaves so. Can you please provide me a solution for my code to work ie being able to run keywords from the object created.

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [28 March 2023 08:15 UTC](https://forum.robotframework.org/t/how-to-access-methods-from-object-after-it-is-saved-in-a-variable/5411/12 "2023-03-28T08:15:05Z")

</div>

Hi Anusha,

> [@adathatri](#):
>
> Can you please provide me a solution for my code to work ie being able to run keywords from the object created.

Pekka (@pekkaklarck) already did that, see his post:

> [@How to access methods from object after it is saved in a variable](https://forum.robotframework.org/t/how-to-access-methods-from-object-after-it-is-saved-in-a-variable/5411/4):
>
> Notice that simply using \*\*\* Settings \*\*\* Library sing.py \*\*\* Test Cases \*\*\* Test ${word}= Name Log To Console ${word} ought to work the same way. Unless you have some specific reason for your approach, this is a lot simpler.

Dave.

---

<div class="post-metadata">

**Author:** ![adathatri](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/adathatri/32/2417_2.png) [@adathatri](https://forum.robotframework.org/u/adathatri)\
**Post date:** [28 March 2023 12:48 UTC](https://forum.robotframework.org/t/how-to-access-methods-from-object-after-it-is-saved-in-a-variable/5411/13 "2023-03-28T12:48:38Z")

</div>

I do not want to import it right at the beginning. I want the object to get initialised only on certain conditions and will need multiple instances of the class sing().

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [28 March 2023 14:40 UTC](https://forum.robotframework.org/t/how-to-access-methods-from-object-after-it-is-saved-in-a-variable/5411/14 "2023-03-28T14:40:30Z")

</div>

Hi Anusha,

Consider the example below where the python file does nothing until one of it’s functions are called. You can do something like this, there’s no need to have an ` __init__ ` function in your py file (it’'s optional), but if you have a some initialisation that needs to happen before the other keyword functions, just put it in a normal function and call it as a keyword before the keyword that needs it inside your if statement.

Dave.

_singa.py_

```python
class singa():
    def Sound_Music(self):
        return "Doe a Deer"
    def Song_ABBA(self):
        return "I am the Dancing Queen"

```

_singb.py_

```python
class singb():
    def DanceA(self):
        return "Bharatanatyam"

    def DanceB(self):
        return "Kathakali"

    def DanceC(self):
        return "Kathak"

    def DanceD(self):
        return "Kuchipudi"

    def DanceE(self):
        return "Odissi"

    def DanceF(self):
        return "Sattriya"

    def DanceG(self):
        return "Manipuri"

    def DanceH(self):
        return "Mohiniyattam"

```

_singc.py_

```python
class singc():
    def Song_CA(self):
        return "brrrr"
    def Song_CB(self):
        return "pt pt pt pt"
    def Song_CE(self):
        return "whrrrrr"

```

_Anusha2.robot_

```plaintext
***Settings***

Library singa.py
Library singb.py
Library singc.py

***Test Cases***
Test
   ${condion}= Evaluate random.randint(0, 13)
   IF ${condion} == 0
   	${word}= Sound Music
	   Log To Console ${word}
	 END
	 IF ${condion} == 1
   	${word}= Song Abba
	   Log To Console ${word}
	 END
	 IF ${condion} == 2
   	${word}= Dancea
	   Log To Console ${word}
	 END
	 IF ${condion} == 3
   	${word}= Danceb
	   Log To Console ${word}
	 END
	 IF ${condion} == 4
   	${word}= Dancec
	   Log To Console ${word}
	 END
	 IF ${condion} == 5
	 ${word}= Danced
	   Log To Console ${word}
	 END
	 IF ${condion} == 6
	 ${word}= Dancee
	   Log To Console ${word}
	 END
	 IF ${condion} == 7
	 ${word}= Dancef
	   Log To Console ${word}
	 END
	 IF ${condion} == 8
	 ${word}= Danceg
	   Log To Console ${word}
	 END
	 IF ${condion} == 9
	 ${word}= Danceh
	   Log To Console ${word}
	 END
	 IF ${condion} == 10
   	${word}= Song Ca
	   Log To Console ${word}
	 END
	 IF ${condion} == 11
	 ${word}= Song Cb
	   Log To Console ${word}
	 END
	 IF ${condion} > 11
	 ${word}= Song Ce
	   Log To Console ${word}
	 END

```

When I ran this I got (condition 10):

```python
==============================================================================
Anusha2                                                                       
==============================================================================
Test ...brrrr
Test | PASS |
------------------------------------------------------------------------------
Anusha2 | PASS |
1 test, 1 passed, 0 failed
==============================================================================

```

---

<div class="post-metadata">

**Author:** ![adathatri](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/adathatri/32/2417_2.png) [@adathatri](https://forum.robotframework.org/u/adathatri)\
**Post date:** [4 April 2023 14:23 UTC](https://forum.robotframework.org/t/how-to-access-methods-from-object-after-it-is-saved-in-a-variable/5411/15 "2023-04-04T14:23:10Z")

</div>

Thanks for your response. I’ll try this solution.
