90 questions
- Bountied 0
- Unanswered
- Frequent
- Score
- Trending
- Week
- Month
- Unanswered (my tags)
-5
votes
1
answer
135
views
How do I fix this error with the walrus operator? [closed]
I'm doing a simple task and my "solution" raised an error. I am using the walrus operator (:=) improperly but I would like to know how I can change my code to use it properly.
Code basically ...
3
votes
5
answers
204
views
Sequence of function iterations
Is there a way to abuse assignment expressions or functional tools to generate the sequence x, f(x), f(f(x)), ... in one line?
Here are some contrived examples to demonstrate:
def iter(x, f, lim=10):
...
-4
votes
1
answer
132
views
For-loop with the walrus operator inside the range
Why are the negative k values being printed as the original value, but when we enter the positive realm, it gets strange?
In:
def j(k):
for i in range(i:=k):
i+=i
print(i)
for i in [-5,...
-3
votes
1
answer
163
views
Python walrus operator. Can I use two != != in walrus operator?
I'm new to programming. I'm learning the walrus operator. I want the while loop end/stop when someone type quit or finish. Only quit works. when someone type "finish", it produces an output ...
2
votes
1
answer
98
views
On this Conditional Expression, what the syntax error about?
I get:
return r.group() if r := re.match(rx,l) else None
^
SyntaxError: invalid syntax
whereas
return r.group() if (r := re.match(rx,l)) else None
is accepted.
What ...
1
vote
2
answers
1k
views
Walrus operator assign variable if method does return None
I read the documentation and lots of examples but do not fully understand how to use it.
I have a function, screen() that returns a screenshot but may return None. I call this in a loop to a variable. ...
1
vote
2
answers
158
views
Can a walrus expression be put inside square brackets rather than parentheses?
Take the following code:
k = 'A'
d = {}
d[k := k.lower()] = 'b'
print(k)
print(d)
This gives the output one would expect:
a
{'a': 'b'}
However, for similar code in a project of mine, flake8 ...
0
votes
1
answer
77
views
Can any usage of the assignment operator technically be replaced with the walrus operator surrounded by parentheses?
Say we have this assignment statement:
a = 5
Even though it'd obviously be ugly to do this for no reason, we could technically accomplish the same thing with:
(a := 5)
This still assigns 5 to a, and ...
-1
votes
1
answer
143
views
Can these sorts of if/else statements be boiled down with a walrus operator?
I have read up blogs and tutorials around the := operator but I am still trying to wrap my head around how to know when to use it. I understand its purpose is to improvement maintainability, avoiding ...
3
votes
1
answer
536
views
Pylint is not suggesting the walrus operator. Why?
I was going to ask if there is a pylint-style code analyzer capable of suggesting the use of the := operator in places were it might improve the code. However, it looks like such test has been added ...
19
votes
2
answers
2k
views
Why do f-strings require parentheses around assignment expressions?
In Python (3.11) why does the use of an assignment expression (the "walrus operator") require wrapping in parentheses when used inside an f-string?
For example:
#!/usr/bin/env python
from ...
0
votes
1
answer
102
views
Find fibonacci series upto N terms using list comprehension
Need to find the Fibonacci series up to N terms.
n=int(input("Enter the n terms: "))
f_val=0
fib_list=[f_val:=f_val + i for i in range(n)]
print(fib_list)
While executing the above program, ...
3
votes
2
answers
315
views
Equivalent of Python's Walrus operator (:=) in Julia
In python you can write
if (n := len(a)) > 10:
print(f"List is too long ({n} elements, expected <= 10)")
is there an equivalent feature in Julia?
-1
votes
1
answer
145
views
Why are those algorithm so different in term of their execution time ? Is the walrus operator less time consuming in Python?
While making a function to manipulate the TypeVar from the typing module, I came across a problem.
My function do work but I wanted to make it faster so I used the timeit module to test some different ...
-3
votes
1
answer
2k
views
What is the difference between := and ::= in Python?
I see Python examples with := and ::=. I've got a good understanding of the walrus operator from an article on RealPython(The Walrus Operator: Python 3.8 Assignment Expressions). I also went through ...