built-in

Why/how does Python's print built-in allow the “>>” operator? [duplicate]

[亡魂溺海] 提交于 2019-12-11 07:54:00
问题 This question already has an answer here : How does the right-shift operator work in a python print statement? (1 answer) Closed 6 years ago . I searched around and couldn't find an answer to this either on this site or elsewhere (always a challenge searching for topics involving punctuation chars). I was looking up the StringIO in the Python standard library (here) and one of the examples is this (excerpt): import StringIO output = StringIO.StringIO() output.write('First line.\n') print >

Python Deap library, how to access initRepeat mapping

∥☆過路亽.° 提交于 2019-12-11 07:28:14
问题 What I want my algorithm to do I want to have a bunch of random words as individuals in my population, whos fitness is compared to an "optimal" word. If the length of the words are equivalent, that's +1 in fitness. For each char that is the same, that's also +1 in fitness, +1.1 if it is in the same index. Unfortunately I'm having a really though time understanding deaps docs. This is what I have tried among other things so far. Attempted Code from deap import creator from deap import tools

Override list's builtins dynamically in class scope

别等时光非礼了梦想. 提交于 2019-12-11 03:49:37
问题 Purely curiosity question: class Li(list): pass m, n= Li([1]), Li([2]) def r(*args, **kwargs): raise Exception('hop') setattr(m, '__iadd__', r) m += n print m # [1, 2] setattr(Li, '__iadd__', r) m += n Output: [1, 2] Traceback (most recent call last): File "C:\...\test_override.py", line 8, in <module> m+=n File "C:\...\test_override.py", line 3, in r def r(*args, **kwargs): raise Exception('hop') Exception: hop If I use setattr(m, 'append', r) then m.append(2) will fail. So is __iadd__

Is it possible to call a built in function from assembly in C++

放肆的年华 提交于 2019-12-11 03:36:46
问题 Considering the following assembly code loop: #include <iostream> #define ADD_LOOP(i, n, v) \ asm volatile ( \ "movw %1, %%cx ;" \ "movq %2, %%rax ;" \ "movq $0, %%rbx ;" \ "for: ;" \ "addq %%rax, %%rbx ;" \ "decw %%cx ;" \ "jnz for ;" \ "movq %%rbx, %0 ;" \ : "=x"(v) \ : "n"(i), "x"(n) \ : "%cx", "%rax", "%rbx" \ ); int main() { uint16_t iter(10000); uint64_t num(5); uint64_t val; ADD_LOOP(iter, num, val) std::cout << val << std::endl; return 0; } Is possible to call a C function (or it's

Why does deleting a global variable named __builtins__ prevent only the REPL from accessing builtins?

左心房为你撑大大i 提交于 2019-12-11 01:17:32
问题 I have a python script with the following contents: # foo.py __builtins__ = 3 del __builtins__ print(int) # <- this still works Curiously, executing this script with the -i flag prevents only the REPL from accessing builtins: aran-fey@starlight ~> python3 -i foo.py <class 'int'> >>> print(int) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'print' is not defined How come the script can access builtins, but the REPL can't? 回答1: CPython doesn't look up _

How to launch a Bash function using Git alias

拈花ヽ惹草 提交于 2019-12-10 20:27:43
问题 I want to use a Git alias in ~/.gitconfig so that it calls a bash function, if it is defined, otherwise call the regular git checkout . This is what I have devised: cat ~/.gitconfig ... [alias] ... co = !(compgen -A function vxzExecuteGitCheckout >/dev/null && vxzExecuteGitCheckout ) || git checkout The problem is that Git uses /bin/sh (which happens to be dash in my case) and it barfs on compgen since it is a bash builtin. Any way making sure that Git uses bash to execute this command? Note

How to dynamically override __setitem__? (no subclass)

有些话、适合烂在心里 提交于 2019-12-10 17:38:31
问题 I'm not able to override some builtin functions, such as '__setitem__', in Python2.7 (although the same occurs in previous versions I tested) Although I know this is easy to do via subclassing, this is not what I want here, I need to be able to dynamically override these methods. Apparently, when my class is a subclass of ' object ', the overridden method always ends up calling the original one, but when my class is not an ' object ', it works: >>> def new_implementation(k, v): ... print 'New

guidelines on using __builtin_expect

允我心安 提交于 2019-12-10 15:42:55
问题 What should I wrap with the gcc's __builtin_expected macros within an if with multiple and nested tests? I have this code: if((x<RADIUS && (forward?v<0:v>0)) || (x+RADIUS>dimensions[d] && (forward?v>0:v<0))) I have (ridiculously) wrapped everything I could: #define likely(x) __builtin_expect((x),1) #define unlikely(x) __builtin_expect((x),0) if(unlikely(unlikely(unlikely(x<RADIUS) && likely(likely(forward)?likely(v<0):likely(v>0))) || unlikely(unlikely(x+RADIUS>dimensions[d]) && likely(likely

Python - os.path doesn't exist: AttributeError: 'module' object has no attribute 'path'

六眼飞鱼酱① 提交于 2019-12-10 13:36:20
问题 Investigating a strange error that I started getting all of a sudden with gdb-python, I reduced it down to this: C:\Users\User>python -i Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win 32 Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> dir(os.path) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'module' object has no attribute 'path' >>> dir(os) ['__builtins__', '__doc__', '__file

How to sum a table of numbers in Lua?

隐身守侯 提交于 2019-12-10 13:24:36
问题 Does Lua have a builtin sum() function? I can't seem to find one, and I've looked almost everywhere in the documentation. Maybe table.sum() , or something of the like, to follow the current conventions. But since I couldn't find it, I had to implement it: function sum(t) local sum = 0 for k,v in pairs(t) do sum = sum + v end return sum end It seems kind of funny to have to implement something this simple, though. Does a builtin function exist, or no? 回答1: I disagree, it would be redundant to