-
Notifications
You must be signed in to change notification settings - Fork 0
How to enable Ruby like method chains in wx(Python) shell
Introduction
In Ruby, everything is an object. Every bit of information and code can be given their own properties and actions as an example below, (https://www.ruby-lang.org/en/about/)
irb> 5.times { print "We *love* Ruby -- it's outrageous!" }
Some of them have criticized Python as an inconsistent language.
For example, Python uses mix of method and functions like sum((1,2,3,4j)).real
.
Ugly? No. Rather, I think, it is the flexibility of Python, no inconsistency.
Theory
I got an idea to make Python chainable like Ruby. To do that, one and only one quite simple rule was required, as follows;
x @ y => y(x)
Hence, we can chain methods as x @y @z => z(y(x))
I named this rule 'pullback' which comes from mathematical terminology.
Actually, this rule can chain not only the instance method but also ANY functions.
Here shows a simple example:
irb> p 5.times.to_a
[0, 1, 2, 3, 4]
>>> 5 @range @list @p
[0, 1, 2, 3, 4]
You may notice that the printing function (p = print) is also chained.
In the next example, you will see that methods and functions in any modules are chained. Suppose 'buf' is the binary data received from the image server. The input and output-code are like these:
# input:
>>> buf @io.BytesIO @Image.open @np.asarray @plt.imshow; plt.show()
# interpreted as:
==> plt.imshow(np.asarray(Image.open(io.BytesIO(buf)))); plt.show()
Also, note that we are pressing [C-j] (eval-expression) each time we chain to see if the py expression is correct.