built-in

Why Is The property Decorator Only Defined For Classes?

烈酒焚心 提交于 2019-12-03 12:12:38
tl;dr: How come property decorators work with class-level function definitions, but not with module-level definitions? I was applying property decorators to some module-level functions, thinking they would allow me to invoke the methods by mere attribute lookup. This was particularly tempting because I was defining a set of configuration functions, like get_port , get_hostname , etc., all of which could have been replaced with their simpler, more terse property counterparts: port , hostname , etc. Thus, config.get_port() would just be the much nicer config.port I was surprised when I found the

MSVC equivalent to __builtin_popcount?

淺唱寂寞╮ 提交于 2019-12-03 11:06:43
问题 What is the equivalent to __builtin_popcount as found in GCC and Clang, for MSVC-10? 回答1: Using the comments provided: __popcnt available through <intrin.h> _mm_popcnt_u64 with SSE4 and <nmmintrin.h> 回答2: With this code snippet you get the GCC builtin when building with MSVC : #ifdef _MSC_VER # include <intrin.h> # define __builtin_popcount __popcnt #endif (Works from Visual Studio 2008). 回答3: The __popcount intrinsic mentioned above doesn't work on ARM, or even all x86 CPUs (it requires ABM

How to add a builtin function in a GCC plugin?

家住魔仙堡 提交于 2019-12-03 10:29:32
问题 It is possible for a GCC plugin to add a new builtin function? If so, how to do it properly? GCC version is 5.3 (or newer). The code being compiled and processed by the plugin is written in C. It is mentioned in the rationale for GCC plugins at gcc-melt.org that this is doable but I cannot see how. As far as I can see in the sources of GCC, the builtins are created using add_builtin_function() from gcc/langhooks.c: tree add_builtin_function (const char *name, tree type, int function_code,

How to restore a builtin that I overwrote by accident?

本秂侑毒 提交于 2019-12-03 09:36:46
I accidentally overwrote set by using it as a variable name in an interactive python session - is there any way that I can get access to the original set function without just restarting my session? (I have so much stuff in that session that I'd rather not have to do that, although of course I can if necessary.) Martijn Pieters Just delete the name that is masking the builtin: >>> set = 'oops' >>> set 'oops' >>> del set >>> set <type 'set'> You can always still access the original built-in through the __builtins__ namespace; use this if you want to override the built-in but want to defer to

Ruby array each_slice_with_index?

こ雲淡風輕ζ 提交于 2019-12-03 05:26:05
问题 If I have arr = [1, 2, 3, 4] I know I can do the following... > arr.each_slice(2) { |a, b| puts "#{a}, #{b}" } 1, 2 3, 4 ...And... > arr.each_with_index { |x, i| puts "#{i} - #{x}" } 0 - 1 1 - 2 2 - 3 3 - 4 ...But is there a built in way to do this? > arr.each_slice_with_index(2) { |i, a, b| puts "#{i} - #{a}, #{b}" } 0 - 1, 2 2 - 3, 4 I know I can built my own and stick it into the array method. Just looking to see if there is a built in function to do this. 回答1: Like most iterator methods,

Extending Math object through prototype doesn't work

不问归期 提交于 2019-12-03 04:56:50
I try to extend JavaScript Math . But one thing surprised me. When I tried to extend it by prototype Math.prototype.randomBetween = function (a, b) { return Math.floor(Math.random() * (b - a + 1) + a); }; In console I have error 'Cannot set property 'randomBetween' of undefined' ... But if I asigne this function to Math.__proto__ Math.__proto__.randomBetween = function (a, b) { return Math.floor(Math.random() * (b - a + 1) + a); }; Then everything works fine. Can anybody explain me why it works in this way? I appreciate any help. Math isn't a constructor, so it doesn't have prototype property:

Why does Python have a format function as well as a format method

烂漫一生 提交于 2019-12-03 04:08:30
问题 The format function in builtins seems to be like a subset of the str.format method used specifically for the case of a formatting a single object. eg. >>> format(13, 'x') 'd' is apparently preferred over >>> '{0:x}'.format(13) 'd' and IMO it does look nicer, but why not just use str.format in every case to make things simpler? Both of these were introduced in 2.6 so there must be a good reason for having both at once, what is it? Edit: I was asking about str.format and format , not why we don

Why can't I use builtin for classes that overload subsref?

落爺英雄遲暮 提交于 2019-12-03 03:28:05
I would like to overload only one type of subsref calls (the '()' type) for a particular class and leave any other calls to Matlab's built in subsref -- specifically, I want Matlab to handle property/method access via the '.' type. But, it seems like Matlab's 'builtin' function doesn't work when subsref is overloaded in a class. Consider this class: classdef TestBuiltIn properties testprop = 'This is the built in method'; end methods function v = subsref(this, s) disp('This is the overloaded method'); end end end To use the overloaded subsref method, I do this: t = TestBuiltIn; t.testprop >>

Why I can call 'print' from 'eval'

▼魔方 西西 提交于 2019-12-03 03:18:17
For code: #!/usr/bin/python src = """ print '!!!' import os """ obj = compile(src, '', 'exec') eval(obj, {'__builtins__': False}) I get output: !!! Traceback (most recent call last): File "./test.py", line 9, in <module> eval(obj, {'__builtins__': False}) File "", line 3, in <module> ImportError: __import__ not found Both 'print' and 'import' are language construct. Why does 'eval' restrict using of 'import' but doesn't restrict 'print'? P.S. I'm using python 2.6 UPDATE: Question is not "Why does import not work?" but "Why does print work?" Are there some architecture restrictions or something

MSVC equivalent to __builtin_popcount?

拟墨画扇 提交于 2019-12-03 01:31:37
What is the equivalent to __builtin_popcount as found in GCC and Clang, for MSVC-10? Using the comments provided: __popcnt available through <intrin.h> _mm_popcnt_u64 with SSE4 and <nmmintrin.h> Eric Nicolas With this code snippet you get the GCC builtin when building with MSVC : #ifdef _MSC_VER # include <intrin.h> # define __builtin_popcount __popcnt #endif (Works from Visual Studio 2008). The __popcount intrinsic mentioned above doesn't work on ARM, or even all x86 CPUs (it requires ABM instruction set). You shouldn't use it directly; instead, if you're on x86/amd64 you should use the _