Giter Site home page Giter Site logo

Comments (5)

craigbarratt avatar craigbarratt commented on May 25, 2024

I don't know why they become unavailable. Is it because the attributes get cleared when you do this?

This seems related to issue #9.

from pyscript.

craigbarratt avatar craigbarratt commented on May 25, 2024

With commit 712692f, state variable setting now preserves attributes.

from pyscript.

fleXible avatar fleXible commented on May 25, 2024

While playing around I came across a different problem regarding overwriting variables.
I seems, that declared variables take presedence over provided variables/objects.

Sample code:

@time_trigger
def precendce_problem():
    log.info(f"precendce_problem: running")
    event = None
    event.fire("raises Exception")

precendce_problem: running
Exception in <jupyter_0.precendce_problem> line 35:
event.fire("raises Exception")
^
AttributeError: 'NoneType' object has no attribute 'fire'

I came across this, when doing a for loop and naming the variable "state" without thinking too much about it.
It wouldn't be too much of an issue, if variables would be scoped, but as they are not, weird things can happen if you don't watch out.

from pyscript.

craigbarratt avatar craigbarratt commented on May 25, 2024

Variable scoping should be the same as Python, and I agree it would be a bug if it wasn't. If you create a local variable it will overwrite a previously defined function or object.

The example looks correct to me. This behaves exactly the same in Python and pyscript:

bytes.fromhex("1234")
bytes = None
bytes.fromhex("5678")

The first call works, and the second one fails with an error:

AttributeError: 'NoneType' object has no attribute 'fromhex'

Here's another example that also works the same in Python and pyscript:

import math
print(math.sqrt(2))
math = None
print(math.sqrt(2))

I agree there is a subtle difference in pyscript: yes, event or state don't exist as an object. But if it does (ie: you set them to something), that takes precedence over the event.fire or state.set functions, which is the same behavior as regular Python.

from pyscript.

fleXible avatar fleXible commented on May 25, 2024

What you say makes sense, I just never happen to stumble upon it before. Issue can be closed.

from pyscript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.