How did they do it? Can I also add my own new operators to Python 3? I searched on google but I did not find any information on this.
No, you can't add your own. The numpy team cooperated with the core Python team to add # to the core language, It's in the core Python docs (for example, in the operator precedence table), although core Python doesn't use it for anything in the standard CPython distribution. The core distribution nevertheless recognizes the operator symbol, and generates an appropriate BINARY_MATRIX_MULTIPLY opcode for it:
>>> import dis
>>> def f(a, b):
... return a # b
>>> dis.dis(f)
2 0 LOAD_FAST 0 (a)
2 LOAD_FAST 1 (b)
4 BINARY_MATRIX_MULTIPLY
6 RETURN_VALUE
Answering your second question,
Can I also add my own new operators to Python 3?
A similar question with some very interesting answers can be found here, Python: defining my own operators?
Recently in PyCon US 2022, Sebastiaan Zeeff delivered a talk showing how to implement a new operator. He warns that the implementation is purely educational though. However, it turns out you actually can implement a new operator yourself! Locally, of course :). You can find his talk here, and his code repository here. And if you think your operator could enhance Python Language, why not propose a PEP for it?
Related
I know about sys.stdout.write() but that uses the sys module,
input("string") woulden't be valid either since it's a built-in function.
It used to be possible in Python 2, because print was a language statement, while it is a built in function in Python 3. But there is only a very tiny difference between statements and built in functions or types. And IMHO trying to avoid built ins is close to non sense in Python.
In Groovy 3.0 (Groovy Version: 3.0.0-rc-1 JVM: 11.0.2)
println 3**3**3
println 3.0**3.0**3.0
gives
19683
19683
In Python (Python 3.5.2) from the terminal, I get
>>> 3**3**3
7625597484987
>>> (3**3)**3
19683
The official site does not give any indication.
Is ** broken in Groovy as of now?
EDIT
Answers in stackoverflow, math.stackexchange make it clear that the the mathematical convention is that exponentiation is right associative.
Defect is defined as a condition in a software product which does not meet a software requirement (as stated in the requirement specifications) or end-user expectation (which may not be specified but is reasonable).
Since this reasonable expectation is violated in an undocumented manner, is not this considered a bug?
No, it's not broken
It just has left to right associativity as with all other operators
In python ** has right to left associativity, so that's not broken either
They just have different rules
I began using PySCIPOpt/SCIP for a Coursera course on discrete optimization. I'd need to implement a simple separation from a fractional variable and wonder how to do it. Online SCIP literature does not provide relevant example.
Any Python example for me to get inspired for my assignment?
Thank you for the answer. Indeed I spent some hours reading SCIP documentation and I have trouble interfacing SCIp methods in Python.
I have been able to implement in Python a simple constraint handler to add first-type cuts and I'd like to add a separator to add second-type cuts.
The latter cuts are typically x = 0 or 1 cuts based on fractional x values and I stumble more with syntax - addCut() - and using generic methods than the process itself.
A Python example, a bit more involved than tsp.py, would greatly help me.
Your question is quite broad. I try to give some hints on where to look for an answer:
general information on separators in SCIP
difference between constraint handlers and separators
Python example on a separator implemented as constraint handler to solve the Traveling Salesman Problem
I can try to answer your question more precisely, if you explain your application/problem in more detail.
I'm using Python 2.7 and am creating an HMAC using the hmac library. Python 3.3 includes a compare_digest() function that will compare two digests and resist timing attacks, but that's not available in 2.7. Prevailing advice is not to roll my own crypto, so are there any mature Python libraries that provide that functionality? PyCrypto does not appear to.
For anyone finding this from search, if using Django, then you can also use the constant_time_compare function in django.utils.crypto.
>>> from django.utils.crypto import constant_time_compare
>>> constant_time_compare("foo", "bar")
False
>>> constant_time_compare("foo", "foo")
True
That this comes with the same caveat as hmac.compare_digest (and actually uses hmac.compare_digest if it exists):
Note: If a and b are of different lengths, or if an error occurs, a timing attack could theoretically reveal information about the types and lengths of a and b–but not their values.
I would suggest you just use the secure compare method available in 3.3.
This is an implementation that is very similar to the Python implementation:
def compare_digest(x, y):
if not (isinstance(x, bytes) and isinstance(y, bytes)):
raise TypeError("both inputs should be instances of bytes")
if len(x) != len(y):
return False
result = 0
for a, b in zip(x, y):
result |= a ^ b
return result == 0
Can't see how that would breach any licenses.
If you have access to Python 2.7.7, compare_digest() was recently backported to this version (as well as the more secure 3.x SSL module in 2.7.9).
https://www.python.org/dev/peps/pep-0466/
What is the most efficient way to generate k-tuples with all k-combinations from a given python set? Is there an appropriate built-in function? Something tells me it should be possible with a two-line for loop.
P.S. I did conduct a search and found various entries to the topic "combinations from lists, etc in Python", but all proposed solutions seem rather 'un-python'. I am hoping for a mind-blowing, idiomatic python expression.
itertools has all of those types of functions:
import itertools
for combination in itertools.combinations(iterable, k):
print(combination)