Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Sharing variable among files?

    Scheduled Pinned Locked Moved Guides
    python
    7 Posts 3 Posters 1.8k Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M Offline
      misterlisty
      last edited by

      If i declare a variable in boot.py is available to all files?

      L 1 Reply Last reply Reply Quote 0
      • L Offline
        livius @misterlisty
        last edited by

        @misterlisty
        yes

        think about people first, then computers

        1 Reply Last reply Reply Quote 0
        • M Offline
          misterlisty
          last edited by

          thank you

          1 Reply Last reply Reply Quote 0
          • M Offline
            misterlisty
            last edited by

            ok, this works fine but it appears that if i chnage the value of this variable within a function then it appears to not be reflected within other function or files? I assume this has something to do with variable scope?

            L 1 Reply Last reply Reply Quote 0
            • L Offline
              livius @misterlisty
              last edited by livius

              @misterlisty

              variable scope

              yes, you must do this with global keyword
              e.g.:

              def myproc():
                  global my_global_var
                  my_global_var = 1

              think about people first, then computers

              robert-hhR 1 Reply Last reply Reply Quote 0
              • robert-hhR Offline
                robert-hh @livius
                last edited by

                @livius That does not work for me. Did you try that?

                L 1 Reply Last reply Reply Quote 0
                • L Offline
                  livius @robert-hh
                  last edited by

                  @robert-hh
                  i was too fast, you have right global is for same unit
                  simple use qualified name e.g:

                  a.py

                  myvar = 7
                  

                  b.py

                  import a
                  
                  def changea():
                  	a.myvar+= 2
                  	print(a.myvar)
                  	
                  def printa():
                  	print(a.myvar)
                  

                  for test use

                  import b
                  b.changea()
                  >>>9
                  b.print(a)
                  >>>9
                  

                  think about people first, then computers

                  1 Reply Last reply Reply Quote 0

                  Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                  Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                  With your input, this post could be even better 💗

                  Register Login
                  • 1 / 1
                  • First post
                    Last post

                  Pycom on Twitter

                  Pycom