Doubts regarding variables

Hi experts

I am just new to robocorp and i need to clear few of my doubts

Like for list variables we specify as @

But for datatable why we are specifying $ before variable name since it is collection

Can anyone help to clear my doubts

Nived N


The list (@{variable}) and dictionary (&{variable}) syntax is optional when assigning variables. You can use the list syntax when assigning a Table to variable, but it is not necessary. I can try to explain the difference between them.

First off, when doing variable assignment it’s entirely valid to assign lists and dictionaries to scalar variables like this:

${my_list}=    Create list    1    2    3
${my_dict}=    Create dictionary    one=1    two=2

The reason to use the special syntax is that Robot Framework does some extra validation for you:

# This will validate that the keyword returns a list (or something list-like):
@{my_list}=    Create list    1    2    3
# This will validate that the keyword returns a dictionary (or something dictionary-like):
&{my_dict}=    Create dictionary    one=1    two=2

With dictionary variables the assignment does another thing, which is it turns it into a Robot Framework DotDict. A DotDict is otherwise identical to a normal Python dictionary, but allows accessing values through dot-notation: ${my_dict.two}.

Note: If you assigned to @{my_list}, you can always access it through ${my_list}, and vice-versa. They refer to the same variable.

The second place where this syntax is used is when calling keywords. With list or dictionary variables they are unpacked as arguments automatically. If you are familiar with the Python syntax *args or **kwargs, this is similar to that.

For example here we call the first keyword in three different ways: with individual arguments, by unpacking a list, and by unpacking a dictionary:

An example keyword
    [Arguments]    ${first_arg}    ${second_arg}
    Log   Got arguments ${first_arg} and ${second_arg}

A second keyword
    ${as_list}=    Create list    1  2
    ${as_dict}=    Create dictionary    first_arg=1    first_arg=2
    # All three of the following will log "Got arguments 1 and 2"
    An example keyword    1    2
    An example keyword    @{as_list}
    An example keyword    &{as_dict}

This is also why it is used when FOR looping. These two examples are identical:

Loop through scalars
    ${name_one}=    Set variable    John
    ${name_two}=    Set variable    Robert
    FOR    ${name}    IN    ${name_one}    ${name_two}    Mark
        Log    Name is ${name}

Loop through list
    ${names}=    Create list    John    Robert    Mark
    FOR    ${name}    IN    @{names}
        Log    Name is ${name}

If the second FOR loop used ${names} instead of @{names}, it would not loop the names inside the list, but would instead only loop once and return the list itself.

So in conclusion: We don’t use the @-syntax when assigning a Table to a variable because it is optional, but it would work because a Table is list-like. We also have to use the @-syntax when looping over a Table, because we want to loop over the rows inside of it.

I hope this clears things up!


Thanks @osrjv for your nice explanation

Thanks for it

Nived N