language-design

Python: Why does the int class not have rich comparison operators like `__lt__()`?

我与影子孤独终老i 提交于 2019-12-22 01:26:12
问题 Mostly curious. I've noticed (at least in py 2.6 and 2.7) that a float has all the familiar rich comparison functions: __lt__() , __gt__ , __eq__ , etc. >>> (5.0).__gt__(4.5) True but an int does not >>> (5).__gt__(4) Traceback (most recent call last): File "<input>", line 1, in <module> AttributeError: 'int' object has no attribute '__gt__' Which is odd to me, because the operator itself works fine >>> 5 > 4 True Even strings support the comparison functions >>> "hat".__gt__("ace") True but

Python Math Regex [closed]

♀尐吖头ヾ 提交于 2019-12-21 22:45:36
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . I am creating a programming language in Python and one main component that I'm missing for the basic language is the ability to parse mathematical equations. I have the function to parse the math but I need to be able to check if the input is a math equation. I'm looking for a regex to match something like 3*x^

Dynamic name resolution

 ̄綄美尐妖づ 提交于 2019-12-21 19:11:13
问题 Howcome some languages like PHP and Python use dynamic name resolution? The only time I've ever thought of using it is to do something like this Python code, to save me from having to explicitly parameters to format : "{a} {b} {c} {d}".format(**locals()) but it doesn't really take much work to just be explicit (and is a bit less error-prone): "{a} {b} {c} {d}".format(a=a, b=b, c=c, d=d) And for setting/getting locals in the same scope, I don't see why anyone would ever use that instead of a

Why can I access private/protected methods using Object#send in Ruby?

Deadly 提交于 2019-12-21 17:02:08
问题 The class class A private def foo puts :foo end public def bar puts :bar end private def zim puts :zim end protected def dib puts :dib end end instance of A a = A.new test a.foo rescue puts :fail a.bar rescue puts :fail a.zim rescue puts :fail a.dib rescue puts :fail a.gaz rescue puts :fail test output fail bar fail fail fail .send test [:foo, :bar, :zim, :dib, :gaz].each { |m| a.send(m) rescue puts :fail } .send output foo bar zim dib fail The question The section labeled "Test Output" is

Overcoming needle haystack confusion in PHP

。_饼干妹妹 提交于 2019-12-21 07:25:28
问题 What is the most practical way of overcoming needle haystack confusion in PHP? Here $needle is the first argument bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] ) Here $needle is the second argument string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] ) 回答1: it might make sense if you think of this as pre-fix representation of an in-fix operations. is "bat" in array ("cat", "rat", "bat", "fat") is $needle in_array $haystack in_array(

Reified generics in Scala on .NET/CLR

可紊 提交于 2019-12-21 02:35:16
问题 Scala (at least on the JVM) uses type erasure for Java compatibility. This feature is widely held to suck. Fixing this would be difficult on the JVM. In contrast to the JVM situation, .NET supports reified generics. Does Scala's .NET implementation use them? If not, could it, or else what issues would using reification cause? 回答1: It's work in progress, carefully not to break Scala semantics between JVM and .NET. I asked this question back in 2011 on the scala-tools mailinglist and the answer

What would we do without NULL?

陌路散爱 提交于 2019-12-20 09:22:37
问题 I once read that having nullable types is an absolute evil. I believe it was in an article written by the very person who created them(in Ada?) I believe this is the article Anyway, so what if by default a language like C# used non-nullable types? How would you replace some of the common idioms in C# or Ruby or any other common language where null is an acceptable value? 回答1: Instead of outright declaring that nullable types are evil, I would posit: most languages graft nullability onto

Why are slices in Python 3 still copies and not views?

白昼怎懂夜的黑 提交于 2019-12-20 08:26:51
问题 As I only now noticed after commenting on this answer, slices in Python 3 return shallow copies of whatever they're slicing rather than views. Why is this still the case? Even leaving aside numpy's usage of views rather than copies for slicing, the fact that dict.keys , dict.values , and dict.items all return views in Python 3, and that there are many other aspects of Python 3 geared towards greater use of iterators, makes it seem that there would have been a movement towards slices becoming

Why do you have to 'import' Python Standard Library functions? [closed]

a 夏天 提交于 2019-12-20 01:39:50
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 6 years ago . I'm new to Python coding and I'm coming from a PHP background. I'm curious why you have to 'import' functions at the top of you python script. In PHP you can simply use the function such as: sleep(10); The above would cause the script to sleep for 10 seconds. However, to do the

Why not a memberinfo() reflection function for C# [duplicate]

浪子不回头ぞ 提交于 2019-12-19 19:55:43
问题 This question already has answers here : Why is there not a `fieldof` or `methodof` operator in C#? [closed] (4 answers) Closed 6 years ago . There is sizeof() and typeof() , but why not a memberinfo() returning an instance of System.Reflection.MemberInfo for the part of code selected in order to aid in reflection code. Example: Program() { Type t = typeof(Foo); Foo foo = new Foo(); PropertyInfo pi = memberinfo(Foo.Name) as PropertyInfo; // or shall it be like this // PropertyInfo pi =