2
I’m having a doubt, is it possible to store commands in variables? For example: X = while X(True):
So I could facilitate some things in some scripts, or kind of trying to "create" a language of my own, hopefully someone can help me! :-)
2
I’m having a doubt, is it possible to store commands in variables? For example: X = while X(True):
So I could facilitate some things in some scripts, or kind of trying to "create" a language of my own, hopefully someone can help me! :-)
5
You can save functions:
def minha_funcao(x):
while True:
x += 1
print(x)
if x % 5 == 0:
break
minha_variavel_func = minha_funcao
minha_variavel_func(2) # Executa minha_funcao com 2 como argumento x
If it fits in a row, you can also use a lambda:
minha_lambda = lambda: print(*[i for i in range(4)])
minha_lambda() # Printa "0 1 2 3"
I think lambda would not fit in this context analyzing conceptually.
If what OP is looking for is a way to create "shortcuts" of a line in Python, lambda does fit, even not solving exactly what he asked.
@hdyogenes It is not just because it is from a line of code that one uses lambda. A function could be defined with def
on a hassle-free line, without breaking code semantics. Lambda defines an input/output ratio. Using it may even generate the expected result, but it doesn’t mean it should be done.
@Andersoncarloswoss Where is it written that lambda defines a relation between input and output? In the official Python tutorial it is written that "Lambda functions can be used wherever function objects are needed." That is, it is a resource as valid as a def
. On the other hand, I think to say that "it would not fit in this context analyzing conceptually" is very subjective, and ends up criticizing the answer without adding any useful alternative. https://docs.python.org/3.5/tutorial/controlflow.html#lambda-Expressions
1
Apparently what you’re trying to do is create a macro, resource that Python does not have. You can try using macropy, a library that implements this, but apparently in a very complicated way (requires manipulation of the Python syntax tree).
If you want to create a kind of DSL (Domain-Specific Language - Domain-Specific Language), it is easier. Python will have some limitations to this, but depending on how far you want to go, it also provides tools, from the use of Lambdas and decorators, to metaclasses, Inspect module, etc.
Another option is to find a language that has the native macros feature, such as LISP or Elixir.
The answer contains a possible solution to the problem, so I don’t see why it should be posted as a comment. In addition, it does not "rely entirely on an external link", it contains other information that may be useful to those who asked, such as the name of the resource he is seeking to use.
@hdyogenes I remember a command called doskey
Win, you could even replace one command with the other and automate your macros ;). Anyway, it would also be interesting for you to add a small practical example of how to use this library.
Browser other questions tagged python-3.x
You are not signed in. Login or sign up in order to post.
It is not clear what you want to do, nor what the goal is.
– hdiogenes
I am wanting to replace commands like while with variables, for example if I want to use the variable x as if it were while
– Caio Cristiano
All right, Caio, let’s go! I can understand what you want in this particular case, but it will be easier to help you if you can clarify what the ultimate goal is. Is creating a language based on Python? For what purpose?
– hdiogenes
At first it was more out of curiosity, but now I’m wondering if I could make a "programming language" in python to make my scripts easier
– Caio Cristiano