0

I would like to do the equivalent of the following in Python:

x = {
    // code here
    return some_value
}

Is there any way to do this in Python without having to define a whole new function?

i.e. This

x = :
    # code here
    return some_value

And not this

def func():
    # code here
    return some_value

x = func()

EDIT: I know that this example was 1 line and I can use a lambda for this specific example, however, I'm wanting to use this for code that requires multiple lines, which is why I cannot use a lambda. I will edit the code to include more than one line.

  • 4
    What do you mean by javascript code block? The snippet you posted is not valid Javascript is it? – Mark Jun 30 '23 at 16:56
  • Have you tried running that JavaScript code in your browser console or in Node? – Pointy Jun 30 '23 at 16:57
  • Could you please provide more context on your end goal? It sounds that you want something such as a lambda function. – AbeCoull Jun 30 '23 at 17:00
  • Do you mean `x = () => {return 1 + 1}` in javascript that might be the same as `x = lambda : 1+1` in python? – JonSG Jun 30 '23 at 17:00
  • 1
    javascript does not yet have [block expressions](https://github.com/tc39/proposal-do-expressions) unlike [rust](https://rustwiki.org/en/reference/expressions/block-expr.html) for example. Python also does not have block expressions – Zachiah Jun 30 '23 at 17:02
  • let me know if you want me to make an answer based on this @jackhart591 – Zachiah Jun 30 '23 at 17:04
  • @jackhart591, you edited the question, but since the javascript is still not valid javascript, it's not at all clear what you mean by "python equivalent". The javascript you showed throws and error, so does the python — maybe they are already equivalent? As mentioned above, in python (and javascript) blocks are statements, not expressions, so they don't produce a value you can assign to something, – Mark Jun 30 '23 at 17:39
  • @Mark The fact that it is not valid Javascript is not important. I'm not a js developer and don't pretend to be. I purposely removed any mention of Javascript for this reason. Code blocks are a general term and I feel this question makes sense even if the pseudocode does not compile in Javascript, yes? :) – jackhart591 Jun 30 '23 at 17:46
  • @jackhart591 You discounted functions already, but they really are the Python solution for this problem. [There are no multi-line lambdas, because those are basically just functions](https://stackoverflow.com/q/1233448/3141234). – Alexander Jun 30 '23 at 17:48
  • @Zachiah yeah go ahead and make an answer, I'll mark it as accepted. Please note my removal of any mention of javascript when writing it. – jackhart591 Jun 30 '23 at 17:49
  • 1
    So your question boils down to: 'does python have block expressions' and the answer is 'no'. Neither does Javascript. – Mark Jun 30 '23 at 17:50
  • 1
    @Mark yes I am aware now, I have had 3 people tell me this now. Thanks. :) – jackhart591 Jun 30 '23 at 17:51
  • @jackhart591 I'm confused about the `return` in the first code block. In languages that _do_ have codeblocks (e.g. C), `return` won't just return a value from the codeblock and assign to `x`. It'll return from the parent function entirely. So I'm not clear here on whether you want these to be blocks of code that run immediately and just provide structure/organization (like a C block), or are blocks of code that isn't evaluated yet, that offers deferred execution (like a Python `lambda`). – Alexander Jun 30 '23 at 17:54
  • @Alexander it was meant to just be pseudocode essentially just to provide better organization. If you are still confused about how this was intended to work, please refer to the last code block which should explain. – jackhart591 Jun 30 '23 at 17:56
  • @jackhart591 Yep, there's no equivalent of that. TBH, even if it did exist, using functions would probably still be preferable. Being forced to name it is a feature, not a bug. And for reference, the Javascript equivalent is an [Immediately invoked function expression(IIFE)](https://en.wikipedia.org/wiki/Immediately_invoked_function_expression). That pattern doesn't work in Python though, because functions declarations aren't expressions. – Alexander Jun 30 '23 at 18:02
  • @Alexander Yup, I'm aware at this point. Thanks :) – jackhart591 Jun 30 '23 at 18:07

2 Answers2

-1

I believe what you're looking for is a lambda. Although please check that your "JavaScript" does work first (though I think this is the concept you're asking for).

For example:

x = lambda : 1 + 1

print(x())

In this case, x stores your "codeblock," or an anonymous function that you can call.

Ryan Zhang
  • 1,856
  • 9
  • 19
-1

You can use lambda,which are anonymous functions.

y=lambda: 1+1
print(y())