Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

[open] Global/local variables and lists

edited December 2012 in OpenSesame

Greetings.

I'm a bit confused about the local/global variable stuff. At the moment, I'm coding three inline_scripts that create random lists, draw from one of them, and use the drawn item for another task, respectively. They are all part of the same trial sequence. In the first script, variables and lists are defined and set globally, and are then used in the two latter scripts.

Here's the first script:

import random
global list1, list2, random

list = [] # create empty list

# append five 1s and 2s to the list:
for i in range(5): 
    list.append(1)
    list.append(2)
    
# define shuffle list function, which takes a list as its argument:
def shuffleList(list): 
    L = list[:] # copy the input list
    random.shuffle(L) # and shuffle the copy
    return L # return shuffled copy

# create 2 shuffled lists:
list1 = shuffleList(list) 
list2 = shuffleList(list)

My questions are:

1) Am I correct in assuming, that I only need exp.set or exp.get if I wish to use these variables and lists in other items, outside this sequence?

2) How come I need to declare random global in order to use it in my shuffleList function?

Best regards,

Jákup

Comments

  • edited 10:09AM

    Hi Jákup,

    Your questions go a lot deeper than you probably thought:

    Am I correct in assuming, that I only need exp.set or exp.get if I wish to use these variables and lists in other items, outside this sequence?

    'normally', yes: for communication between multiple items (or the 'run' and 'prepare' phases for that matter) can best be done through exp.get() and exp.set(). However, in this thread Lotje mentioned how Opensesame treats list variables: as strings (they're not ints or floats). This is somewhat problematic if you wish to convert them back to their original types. E.g. In 0.26, I tested setting:


    prepare: exp.set('mylist',[1,2,3,4]) run: x = exp.get('mylist')

    which crashed because OS attempted to evaluate '[1,2,3,4]' as one bracket-enclosed variable.

    The solution would be to circumvent the get and set methods, by simply creating your variable in the exp object directly, e.g.


    prepare: exp.mylist = [1,2,3,4] run: x = exp.mylist

    Some words of warning though:
    • variables set as such will not be logged by the logger

    • make sure your variable names do not overwrite critical OS fields: for example don't say exp.width = 15 because this overwrites OS's variable for the screen width.

    As for your second question;

    How come I need to declare random global in order to use it in my shuffleList function?

    Internally, your script is not run as a python program. Rather, it is represented as one string, and then the functions inline_script.prepare() and inline_script.run() compile and execute this string by calling python's compile() and exec() functions.

    This works -almost- the same, except for some limitations. Because you're running things from within a function (prepare() or run()), modules and variables are defined with only a local scope, i.e. defined within that function. If you now define another function within this function (i.e. your shuffleList function), python would usually transport these local variables and modules into the inner function.
    However, with exec() this doesn't happen (here's a thread to get you started on why ). Local variables and modules are not passed onto the inner function, and therefore random is not defined in this function.

    You can see this in a python terminal, using the locals() and globals() functions:


    def comp_and_run():
    exec('import random\ndef fun():\n print locals();\n print random.choice([5,6])\n\nfun()') comp_and_run();

    yields:

    {} # i.e. locals() is empty in the inner function! and then an error: Traceback (most recent call last):
    File "bla.py", line 10, in <module>
    comp_and_run();
    File "bla.py", line 7, in comp_and_run
    exec('import random\nprint locals(); print globals()\ndef fun():\n print locals();print globals();\n print random.choice([5,6])\n\nfun()')
    File "<string>", line 7, in <module>
    File "<string>", line 5, in fun
    NameError: global name 'random' is not defined

    Whereas when running the same program as 'regular code'


    def comp_and_run():
    import random
    def fun():
    print locals();
    print random.choice([5,6])
    fun() comp_and_run()

    runs fine, yielding:

    {'random': (...and then some more...)}
    ** # i.e. the locals have been copied into fun() ** and then it runs fine: 6

    ...because as you can see, the 'locals' are transported into fun.

    it's pretty technical, but I hope this clarifies matters a bit!

    As for your program; if you don't want to type (shuffle) the whole time, you can always define a for-loop, right?


    import random list1 = [1,2] * 5 # a python 'trick', easier than appending with a loop
    list2 = list1[:] # deep copy of list1 for l in [list1, list2]:
    random.shuffle(l) and setting them in exp: exp.my_list1 = list1
    exp.my_list2 = list2

    Hope this helps,

    Wouter

  • edited December 2012

    Dear Wouter.

    Thank you very much for your excellent explanation! This really cleared things up for me. You are right in saying, it ran deeper than I had imagined! I actually thought long about whether or not I should even post, as the questions seemed almost too simple. :)

    And thanks for the 5x multiplication trick instead of appending. Another great example of how elegant Python can be :)

    Best regards,

    Jákup

  • Try this detailed tutorial on...python list

Sign In or Register to comment.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games