[solved] dynamically scoped vs lexically scoped

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
Ekamu
Regular
Posts: 130
Joined: Mon Aug 12, 2013 9:13 am
Completed: Paper Guitar
Location: Lucid Moon
Contact:

[solved] dynamically scoped vs lexically scoped

#1 Post by Ekamu »

So in the documentation it says something about variables being dynamically scoped for labels and calls.

Labes and Control Flow

Wikipedia says this about dynamically scoped and lexically scoped variables.
Dynamically Scoped
A fundamental distinction in scoping is what "part of a program" means. In languages with lexical scope (also called static scope), name resolution depends on the location in the source code and the lexical context, which is defined by where the named variable or function is defined.

Lexically Scoped
Lexical scoping (sometimes known as static scoping ) is a convention used with many programming languages that sets the scope (range of functionality) of a variable so that it may only be called (referenced) from within the block of code in which it is defined.

Whay does this mean in 'Human English' (without all the jargon like name resolution and lexical content). I'm guessing dynamic scoped variables are like global variables and lexically scoped variables are like local variables.

I've only herd of dynamically scoped variables in Lua and in Lua everything is global by default. Is it the same in Renpy/Python2.7
Last edited by Ekamu on Wed Mar 16, 2016 6:18 am, edited 1 time in total.
------------Paper Guitar------------
Image
Experimental Nostalgic Romance VN Out Now!
.......made with love from the lucid moon........

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: dynamically scoped vs lexically scoped

#2 Post by xela »

Ekamu wrote:I'm guessing dynamic scoped variables are like global variables and lexically scoped variables are like local variables.
Not exactly, but that could be one way of looking at it.

Dynamic scope is possible in LUA, just like in Python but you'd have to take special measures to get that done. Both PLs are considered lexically scoped (along with most modern languages). That "Dynamic" bit is prolly there due to existence of "stack depth" when calling the labels.

It's not easy to explain in simpler terms but what you need to look out for is this:

Code: Select all

default var = 10

init python:
    def func():
        return var + 5

    def func2():
        var = 100
        return func()

label start:
    $ result = func()
    "[result]"
    $ result = func2()
    "[result]"
    return
In any conceivable case, without extraordinary measures taken and var remaining the same, func will always return the same 15 answer. And yet, in paramiterized labels:

Code: Select all

default var = 10

init python:
    def func():
        return var + 5

label start:
    $ result = func()
    "[result]"
    call called
    return
    
label called(var=5):
    $ result = func()
    "[result]"
    return
inside of the label called, func returns 10 instead of 15 as the var is resolved at run-time and has a higher priority for a lookup than the global var with the same name. It should also be noted that the global var in store is not overwritten with this.

Basically you cannot really count on var being bound to something when you write the code as it's evaluation during the run-time may tweak and twist it's value depending on many factors. This can (and prolly will) lead to hard to find errors (which is prolly why I almost never use it).

Not all code in those labels "magically" becomes dynamically scoped. For example, if it trully was so, one might expect func2 to return 105 instead of 10 when called in label "called" but that is not a case.


=====>>
Just for the record, if you don't understand something, Ren'Py makes it very easy to test stuff as any small project created to illustrate one issue can be ran and tested quite easily. If you don't understand why something is happening or get an unexpected result, that's when you go Q&A :)

I know that from experience, when you try stuff yourself, it will give you more experience and confidence than any given answer.
Like what we're doing? Support us at:
Image

User avatar
Ekamu
Regular
Posts: 130
Joined: Mon Aug 12, 2013 9:13 am
Completed: Paper Guitar
Location: Lucid Moon
Contact:

Re: dynamically scoped vs lexically scoped

#3 Post by Ekamu »

O.K I get this sort of like closure in Lua, every function looks up to an up-value, so if you have a value that's declared in the function (like the call example where var=5) then it gets that var and forgets the global var. Its like the var is independent for that function. If nothing is declared then it looks up to the last instance of var.

I know it gets a lot more complex than that but I think I get your point, its better to test it out with Renp'y. Some things are hard to explain but easier to show.

Thanks :D .
------------Paper Guitar------------
Image
Experimental Nostalgic Romance VN Out Now!
.......made with love from the lucid moon........

Post Reply

Who is online

Users browsing this forum: Bing [Bot]