How to share global variables between python files

Forum Forums Visual Components 4.1 Python Programming in VC How to share global variables between python files

This topic contains 5 replies, has 6 voices, and was last updated by Avatar deadeye 4 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #10736
    Avatar
    shushu
    Participant

    I have one task list (tasks = [task1,task2,task3]) to be executed using AGV caller, however new task will be assigned using tasks.append(new task) as well when running the current task list. How can two scripts using the same global variable. I tested the import X.py module like below doesn’t work.

    ===============================================

    File: config.py

    x =0 # Default value of the ‘x’ configuration setting
    File: mod.py

    import config
    config.x = 1
    File: main.py

    import config
    import mod
    print config.x

    ==============================================

    #10754
    zesty
    zesty
    Moderator

    My only advice would be use a component property or add a property to an object like a behavior that can be accessed by getting the object.

    #10774
    Avatar
    Ralle
    Participant

    This is a feature that is really missing and it is on my personal feature wish list since several years. At the moment it is only possible to share values of component properties or to send signals between the scripts as zesty already mentioned.

    The only way that succeed for me without using that sharing is to use the ctypes module and working with ctypes pointers from integer addresses. But for a clear working you need a lot of overhead to organize the python objects.

    #11452
    Johnny
    Johnny
    Participant

    I agree something like that would be awesome!

    #13403
    Avatar
    patricblues
    Participant

    Check this…Python Global Variables across modules

    #16102
    Avatar
    deadeye
    Participant

    I started ~ two weeks ago with VisualComponents.
    Maybe it helps, if you use a class to automatic add Properties
    to a Component. This is what I have done:

    By the way, it sucks, that there is still Python 2.7 syntax used.
    I want format strings, I want typehints, I want asyncio, I want modern Python!

    #16690
    Avatar
    galewinston
    Participant

    In Python, it is better to use a single module to hold all the global variables you want to use and whenever you want to use them, just import this module, and then you can modify that and it will be visible in other modules that do the same.

    Create a Global module

    Create a Python program file to access global variable

    Create another Python program to test value is changed or not

     

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.