keith-packard / snek

Snek programming language for tiny systems
GNU General Public License v3.0
292 stars 30 forks source link

All modules should automatically be from module import * #88

Open mobluse opened 2 months ago

mobluse commented 2 months ago

It would be easier if from math import * was used because it would save people from having to type math. before every math function. All modules are auto imported, and I suggest all modules should also be auto from-imported. That means you could use all functions without a module name prefix. You could still use the fully qualified name if you want or need to. If one name shadows another you have to use the fully qualified name except for the last from-imported module.

The modules could be auto from-imported in an order so that the most used modules are imported last.

from ANYTHING import * should be ignored like import ANYTHING is ignored now.

I think this would be helpful for beginners, since you don't have to type many module prefixes. This means Snek would be more competitive as a first computer language. This would also make Snek programs shorter in flash memory.

E.g. stdscr.refresh() would still need stdscr. since stdscr is an object and not a module.

keith-packard commented 2 months ago

snek doesn't actually support python modules, the whole hack with the math functions is that they really are named math.sin et al. . is part of the name, it's not separate at all. Maybe those functions should also be available without the math. prefix as well, so that you could pretend that from math import * did what you expected?

mobluse commented 2 months ago

I understand that the modules are faked, but I think it should work as if this had been in a Python3 file before the rest of the program:

import eeprom
import tone
import curses
import time
import random
import math
from eeprom import *
from tone import *
from curses import *
from time import *
from random import *
from math import *

I.e. the most common modules should be from-imported last in order to override earlier names.

stdscr is an auto created object in the Snek case.

GPIO functions, e.g. talkto() and on(), are moduleless and temperature() also. Is it reset() or eeprom.reset()? See https://sneklang.org/doc/snek.html#_reset .

merwok commented 2 months ago

62 names (from the math module) seem a bit much, IMHO it’s cleaner and closer to Python to have only math.x names

mobluse commented 2 months ago

It's standard on graphing calculators with Python to from-import math and other modules. It makes programming easier and programs shorter.