-
Notifications
You must be signed in to change notification settings - Fork 371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automatic imports can shadow the user's assignments in the REPL #791
Comments
I'm slightly curious as to why you're overriding built-in Python functions. That's almost never, ever a good idea. Would it kill you to import it as |
but I'm sure we can agree it's unexpected behavior. why should there be some "magic variables" that can't be overridden in the repl? |
Can this be closed now? This actually works in actual programs. |
@jaredly, I agree it's surprising behavior and should be corrected. You can override built-in Python functions in Python. >>> sum
<built-in function sum>
>>> sum = 1
>>> sum
1 @kirbyfan64: Given that Hy is a Lisp-1 without Clojure's namespaces, and without Scheme's Hygienic macros either, I can see why you think the ability to override builtins is a really bad idea: => (defmacro sums [&rest xs] `(sum ~xs))
=> (sums 1 2 3)
6
=> (def sum 1)
=> (sums 1 2 3)
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: 'int' object is not callable
=> But actually, Python's functions have the same issue: >>> def sums(*xs):
... return sum(xs)
...
>>> sums(1,2,3)
6
>>> sum = 1
>>> sums(1,2,3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in sums
TypeError: 'int' object is not callable
>>> Python doesn't have true globals, only module-level globals. Redefining builtins won't break functions using them from another module. Macros are different since they expand into code executed in the present module. This is a good argument for adding something like Clojure's namespace qualification in syntax quotes (probably using Python's module system), but it's not such a good argument against redefining builtins. Hy also disallows reassignment of special forms: => (def if 1)
File "<input>", line 1, column 6
(def if 1)
^-^
HyTypeError: b"Can't assign to a builtin: `if'"
=> But at least it gives you a clear error messsage instead of failing silently like the example in the op. In Clojure, you don't even have to include the core (builtins) in a new namespace (module). |
@gilch Two ideas:
|
Does Hy always import everything in every script, or only the things it needs for that script? If we do do everything up front, then I wonder if there's a way to make |
I'm also concerned about how this might affect Hy's ability to support doctests #1019. We might need to be able to eval (exec?) Hy code from strings to make this work. |
Paul pointed out that you can hit this with a standard Python module,
|
Python's builtins are in the But I'm reluctant to do it that way. The problem is that importing One way to mitigate the damage would be to use Clojure-style namespacing of symbols. #277 We'd then only put It's a real option. But there might be a way to create a Suppose if all Hy modules had a custom I was considering something like this anyway for dynamic variables hylang/hyrule#51. With dynamic variables we could even have the |
map
and filter
(and maybe others?)
The previously mentioned functions (e.g.,
|
I found a problem when I tried to use the curried version of
map
from the Toolz package.In the Hy REPL:
As you can see, I can use the curried function only on the same line as the import.
IPython gives a more explicit error:
The problem is that there is an automatic import of the original
map
function, which replaces the curried version that was previously imported. The same thing happens withfilter
.The text was updated successfully, but these errors were encountered: