Create a calculations operator in python [duplicate] - python

This question already has answers here:
New operators in Python
(4 answers)
Closed 5 years ago.
So I am developing a class for vectorial calculations, and I overwrote the __mul__(self, b) function to do a Scalarproduct. Now whenever I write A * B it calculates as I want it to. So I would like to do the same with an x for the Crossproduct. Sadly there is no default x operator in python, it would probably just annoy you. But is there a way to create your own operator which ONLY works for my own class, but can otherwise be used in a code aswell (as a variable definition I mean)? Or maybe define *** as an operator?

Instead of just define an operator, you can write your own interpreter for your own language inside python. Basically, you need to create 4 modules
parser
environment
eval
repl
Then you can program with your own language within python.
read Peter Novig's article for a detailed example of a python LISP interpreter

Related

How can I find the builtin function code? [duplicate]

This question already has answers here:
Finding the source code for built-in Python functions?
(8 answers)
Closed 2 years ago.
I just want to take a look builtin function code. Because I'm a beginner on Python and I think some source code can give me very useful instruction. I made some test code as follows and I did 'Ctrl+click' on 'join' with PyCharm IDE.
zip_command = "zip -r {0} {1}".format(target, ' '.join(source))
And then cursor points builtin.py module's join function, but there is empty code. There is only an explanation. How does this operate? Where is the real code?
def join(self, ab=None, pq=None, rs=None): # real signature unknown; restored from __doc__
"""
Concatenate any number of strings.
The string whose method is called is inserted in between each given string.
The result is returned as a new string.
Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'
"""
pass
'builtin.py' path is : C:\Users\admin.PyCharmCE2019.3\system\python_stubs\542861396\builtins.py
str.join() is implemented in C, specifically in unicodeobject.c at unicode_join.
"How can I find the source code for builtin functions and objects" doesn't have a great answer. See Finding the source code for built-in Python functions? for some overviews of how CPython is laid out. While some of Python's standard library is written in Python (this sits in lib/), you'll find that builtins and some performance-sensitive components of the standard library have a C implementation. The former resides in objects/, and the latter in modules/.

# operator at the beginning of the line in python [duplicate]

This question already has answers here:
What does the "at" (#) symbol do in Python?
(14 answers)
Closed 4 years ago.
I'm trying to understand what does this operator #, uses in python.
I saw something about matrix multipication , but this is sure not the case,I'll give an example:
#property
def num_reserved_ids(self):
return 0
Or:
#registry.register_problem()
class LibrispeechNoisy(Librispeech):
Last one:
#registry.register_hparams
def transformer_librispeech_tpu_v1():
"""HParams for training ASR model on Librispeech on TPU v1."""
hparams = transformer_librispeech_v1()
update_hparams_for_tpu(hparams)
Registry is another file that is used in the program.register_hparams is a function inside.
Don't know what "property" is, but even registry that I know what it is, I can't understand the purpose of the operator :#, I'm a bit slow, sorry for that :/ ..
If anyone wants to look for some more code you can check tensor2tensor library:
https://github.com/tensorflow/tensor2tensor/tree/master/tensor2tensor
I think what you are looking for is called PythonDecorators
Here is the Wiki For Python Decorators
A Python decorator is a specific change to the Python syntax that allows us to more conveniently alter functions and methods (and possibly classes in a future version). This supports more readable applications of the DecoratorPattern but also other uses as well.
The best way to understand them is from Corey Schafer's Video on Python Decorators

Isn't the keyword "and" an operator in python? Why? [duplicate]

This question already has answers here:
Why is operator module missing `and` and `or`?
(4 answers)
Closed 4 years ago.
I couldn't find the official glossary of python operators but at least it seems that operator library of python doesn't include and or or keyword. They do have operator.and_, but it's for bitwise and operator(&). What makes me more confused is that they do include is or not keywords as operators.
In short, isn't and(and or) an operator? If it isn't, what is the standard of being an operator in Python? I though I'm pretty familiar with python but this problem confuses me a lot at the moment.
Yes, it's an operator. But it's not like the other operators in the operator module.
and short-circuits (i.e., it evaluates its second argument only if necessary). or is similar. Therefore, neither can be written as a function, because arguments to functions are always fully evaluated before the function is called. In some cases this would make no real difference, but when the second argument contains a function call with side effects (such as I/O), or a lengthy calculation, and's behavior is very different from that of a function call.
You could implement it by passing at least the second argument as a function:
def logical_and(a, b):
if not a:
return a
return b()
Then you could call it as follows:
logical_and(x, lambda: y)
But this is a rather unorthodox function call, and doesn't match the way the other operators work, so I'm not surprised the Python developers didn't include it.

What's the difference between non-pure and pure functions? [duplicate]

This question already has answers here:
Why are "pure" functions called "pure"? [closed]
(5 answers)
Closed 6 years ago.
Can pure functions take an argument? For example,
def convert(n):
Thank you in advance
Of course they can have arguments. The only difference is whether they have side effects beyond the input and output parameters. Without input arguments to use as "inspiration", it's difficult for a pure function to do something useful.
Yes they can have arguments. Find some details below:
Pure functions: Functions have some input (their arguments) and return some output
(the result of applying them). The built-in function:
>>> abs(-2)
gives the result:
2
No effects beyond returning a value.
Non-pure functions: In addition to returning a value, applying a non-pure function can generate side effects, which make some change to the state of the interpreter or
computer. A common side effect is to generate additional output beyond the return
value, using the print function.
print(1, 2, 3)
1 2 3

python 3.4.1 itertools documentation syntax lambda( x,_:r*x*(1-x) [duplicate]

This question already has answers here:
What is the purpose of the single underscore "_" variable in Python?
(5 answers)
Closed 8 years ago.
The python 3.4.1 Functional Programming documentation provides examples of itertools. It is section 10.1 under the Functional Programming section 10.0. There is a lambda function defined with the syntax: (The value of r is set elsewhere in the example.)
lambda(x,_:r*x*(1-x))
I have not seen the _ syntax as above. Can someone explain the _ please. In Erlang the _ would match a "don't care". I don't know what it does here.
Thank you.
_ is a variable like any other. The name _ is typically used for variables we don't care about, though this is just a convention; the value isn't automatically discarded or anything.

Categories