literals

What's the advantage of having multi-line & single-line string literals in python?

女生的网名这么多〃 提交于 2019-12-02 01:11:52
I know the triple quote strings are used as docstrings, but is there a real need to have two string literals? Are there any use case when identifying between single-line & multi-line is useful. in Clojure we have 1 string literal, is multi-line and we use it as docstring. So why the difference in python? The advantage of having to be explicit about creating a multi-line string literal is probably best demonstrated with an example: with open("filename.ext) as f: for line in f: print(line.upper()) Of course, any decent syntax-highlighting editor will catch that, but: It isn't always the case

When do (not) two strings with same content share the same memory?

我们两清 提交于 2019-12-02 01:07:05
问题 Coming from the question will two strings with same content be stored in the same memory location? Having the Java code String s1="Java"; will this string be allocated in the same memory location (or multipe): if to launch the same program multiple times executing it in parallel (concurrently)? Possible answer: I am currently C# developer (though programmed in Java in the previous millennium). I asked this question because I believed it is the same between .NET CLR and Java (JVM) and I was

why must use const reference when reference to literals

旧城冷巷雨未停 提交于 2019-12-01 22:07:08
I know only object could have reference. But literals are not object. So I could understand why following code cannot compile: int &a = '4'; int &b = 2; However, when I add const in front of them, it could work!! const int &a = '4'; const int &b = 2; I do not know why. Could anyone help me? A integer or character literal is a prvalue [expr.prim.general] A literal is a primary expression. Its type depends on its form (2.13). A string literal is an lvalue; all other literals are prvalues. Since it is a prvalue we are allowed to take a const & to it but we cannot take a reference to it. If we

When do (not) two strings with same content share the same memory?

柔情痞子 提交于 2019-12-01 21:30:25
Coming from the question will two strings with same content be stored in the same memory location? Having the Java code String s1="Java"; will this string be allocated in the same memory location (or multipe): if to launch the same program multiple times executing it in parallel (concurrently)? Possible answer: I am currently C# developer (though programmed in Java in the previous millennium). I asked this question because I believed it is the same between .NET CLR and Java (JVM) and I was hoping to get the answer for .NET apps (but somehow was in doubt by frequently encountered "application"

What does “string literal in condition” mean?

穿精又带淫゛_ 提交于 2019-12-01 18:12:07
Whenever I try to run the program, an error pops up saying "string literal in condition (on line 10)". What am I doing wrong? puts "Welcome to the best calculator there is. Would you like to (a) calculate the area of a geometric shape or (b) calculate the equation of a parabola? Please enter an 'a' or a 'b' to get started." response = gets.chomp if response == "a" or "A" puts "ok." elsif response == "b" or "B" puts "awesome." else puts "I'm sorry. I did not get that. Please try again." end You have to specify the full condition on both sides of the or . if response == "a" or response == "A"

Literal types: 0x1ull vs 0x1llu

落爺英雄遲暮 提交于 2019-12-01 18:06:44
My gcc compiler allows me to define an unsigned long long (i.e. 64-bit) literal as #define A_LITERAL 0x1ull --- or --- #define A_LITERAL 0x1llu Is there any difference between these two literal statements. Is this common to other C compilers? Both are allowed by the C standard (section 6.4.4.1). The unsigned suffix u can be before or after the long l (or long long ( ll )) suffix. Mats Petersson Both are the same: excerpt from n3337 draft of C++11 standard: integer-suffix: unsigned-suffix long-suffix(opt) unsigned-suffix long-long-suffix(opt) long-suffix unsigned-suffix(opt) long-long-suffix

How does intern work in the following code?

我们两清 提交于 2019-12-01 17:55:52
String a = "abc"; String b = a.substring(1); b.intern(); String c = "bc"; System.out.println(b == c); The question might be foolish as intern has no major usage here, still I am confused about the fact, why does b == c results true . When String b = a.substring(1) is executed, String b references to object having "bc" Does b.intern create the literal "bc" in String Constant pool, even if it does, how come b==c result in true ? String b = a.substring(1); returns string instance which contains "bc" but this instance is not part of string pool (only literals are by defaults interned, string

Python Literal r'\\' Not Accepted

孤街浪徒 提交于 2019-12-01 14:59:55
r'\' in Python does not work as expected. Instead of returning a string with one character (a backslash) in it, it raises a SyntaxError. r"\" does the same. This is rather cumbersome if you have a list of Windows paths like these: paths = [ r'\bla\foo\bar', r'\bla\foo\bloh', r'\buff', r'\', # ... ] Is there a good reason why this literal is not accepted? This is in accordance with the documentation : When an 'r' or 'R' prefix is present, a character following a backslash is included in the string without change, and all backslashes are left in the string. For example, the string literal r"\n"

Is order of a Ruby hash literal guaranteed?

感情迁移 提交于 2019-12-01 14:56:26
Ruby, since v1.9, supports a deterministic order when looping through a hash; entries added first will be returned first. Does this apply to literals, i.e. will { a: 1, b: 2 } always yield a before b? I did a quick experiment with Ruby 2.1 (MRI) and it was in fact consistent, but to what extent is this guaranteed by the language to work on all Ruby implementations? Jörg W Mittag There are couple of locations where this could be specified, i.e. a couple of things that are considered "The Ruby Language Specification": the ISO Ruby Language Specification the RubySpec project the YARV testsuite

Why is the default type of Java integer literals int instead of long? [closed]

ε祈祈猫儿з 提交于 2019-12-01 14:46:34
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . I'm confused why Java integer literals default to int instead of long . This seems to cause unnecessary confusion. First, it requires