Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between the calling methods of Ruby and Python

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly talks about "what is the difference between Ruby and Python calling methods". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what's the difference between Ruby and Python?"

Code Block

Python's lambda / list comprehension can only write one expression for calculation, which makes the language expression ability lag behind greatly. When you need contextual execution, you can occasionally make do with the with statement, and then you can only def the definition before using it.

On the other hand, Ruby has proc support, which has a wider range of applications, and solves the this binding problem that JavaScript has to endure.

Combined with the previously mentioned implicit self and instance_exec, this implements Dynamic Scoping almost in a Static Scoping language, resulting in Internal DSL everywhere in the Ruby community.

Switch-case

The case-when statement of Ruby does not need to write a C-like break, so it will not be fall-through by default. Python makes it clear in a PEP that it does not support it. Of course you can use weird hack to forge one.

You can say, "switch-case should always reconstitute polymorphism," but I want to reserve the right to choose. It's not a big problem to use it in one-off code, or to use it temporarily when you feel that refactoring doesn't benefit much.

So Many More

Ruby also has plenty of language features that allow you to type less:

Does anyone remember the literal amount of regular expressions? it doesn't matter when you don't need it, and you only remember it when you need it.

Method calls and definitions can remove parentheses on both sides of parameters, and in CQS pattern, query with no parameters and all command methods can benefit.

The literal quantity of% w array beats "foo bar" .split () ideographically.

List comprehension is not more ideologically explicit than Enumerable#map/select, and when using both map and filter, the order in which the code is read and executed does not match at all.

Many statements in Ruby can be used as expressions, and the method definition implicitly returns the last expression value. You can write a lot less about variable assignments or return.

Rescue can be found directly in def. Used in the code block of end, there is no need to write the beginning of begin.

String interpolation. Python has two semi-subschemes of% and str.format () to make do. Who said Python advocated using only one of the best solutions?

Ternary operators are not unfamiliar to C language users. Python wants to come up with something counterintuitive like An if P else B instead. Developers are required to type a lot of words. In the real world, people often replace it with a loophole like P and An or B.

Super is the keyword in Ruby and supports direct pass through as well as explicitly passing parameters up the inheritance chain.

such as

Python class Foo (namedtuple ("Foo", "bar baz")): def _ init__ (self, * a, * * kw): super (Foo, self). _ _ init__ (* a, * * kw)

On the contrary, the way Ruby is written... The last line of the previous code block just needs to write super. You don't need to write the name of the current class explicitly, you don't need to pass in self, you don't need to remember the order of the two parameters self and class, you don't need to repeat the name of the method, and you don't need to pass the parameters in one by one. The gap is too big.

You can say that modern editors / IDE offer ways such as snippet to let you type less, but don't forget to be forced to deal with these reduced signal-to-noise ratio boilerplate when reading the code. And we all know how hard it is to maintain code that is difficult to read.

At this point, I believe you have a deeper understanding of "what is the difference between Ruby and Python?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report