built-in

How does Python distinguish explicitly passed None as argument in built-ins

ε祈祈猫儿з 提交于 2020-01-04 02:34:26
问题 I experimented with the next code: >>> f = object() # It's obvious behavior: >>> f.foo Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'object' object has no attribute 'foo' # However, the next one is surprising me! >>> getattr(f, 'foo') Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'object' object has no attribute 'foo' # And this one returns None as expected: >>> getattr(f, 'foo', None) Then I found this pseudo

__sync_val_compare_and_swap vs __sync_bool_compare_and_swap

核能气质少年 提交于 2020-01-02 05:30:11
问题 I've been thinking about the return values of these two functions. The __sync_bool_compare_and_swap function's return value seems to have obvious benefits, i.e. I can use it to tell whether the swap operation took place. However I can't see a good use of __sync_val_compare_and_swap's return value. Firstly, lets have a function signature for reference (from GCC docs minus the var args): type __sync_val_compare_and_swap (type *ptr, type oldval type newval); The problem I see is that the return

In bash, how to get the current status of set -x?

╄→гoц情女王★ 提交于 2020-01-01 07:39:06
问题 I would like to set -x temporarily in my script and then return in to the original state. Is there a way to do it without starting new subshell? Something like echo_was_on=....... ... ... if $echo_was_on; then set -x; else set +x; fi 回答1: You can check the value of $- to see the current options; if it contains an x, it was set. You can check like so: old_setting=${-//[^x]/} ... if [[ -n "$old_setting" ]]; then set -x; else set +x; fi 回答2: Or in a case statement case $- in *x* ) echo "X is set

Extending Math object through prototype doesn't work

老子叫甜甜 提交于 2020-01-01 01:25:06
问题 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

Can I overload operators for builtin classes in Python?

早过忘川 提交于 2019-12-31 05:31:28
问题 Is it possible to overload an operator for a builtin class in Python 3? Specifically, I'd like to overload the + / += (i.e: __add__ operator for the str class, so that I can do things such as "This is a " + class(bla) . 回答1: You can't change str 's __add__ , but you can define how to add your class to strings. I don't recommend it, though. class MyClass(object): ... def __add__(self, other): if isinstance(other, str): return str(self) + other ... def __radd__(self, other): if isinstance(other

main loop 'builtin_function_or_method' object is not iterable

你说的曾经没有我的故事 提交于 2019-12-29 08:49:11
问题 I get this error "main loop 'builtin_function_or_method' object is not iterable" when I run the code below: I have search stackoverflow, but cant find a answer to my question... I have checked for typos, but cant find any error. Please help me! import urllib2 import time import datetime stocksToPull = 'AAPL','GOOG','MSFT','CMG','AMZN','EBAY','TSLA' def pullData(stock): try: print 'Currently pulling',stock print str(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S'))

What does shift() do in Perl?

痴心易碎 提交于 2019-12-28 03:31:16
问题 What could the following line possibly mean? my $x = shift; 回答1: shift() is a built in Perl subroutine that takes an array as an argument, then returns and deletes the first item in that array. It is common practice to obtain all parameters passed into a subroutine with shift calls. For example, say you have a subroutine foo that takes three arguments. One way to get these parameters assigned to local variables is with shift like so: sub foo() { my $x = shift; my $y = shift; my $z = shift; #

Sorting Numericly and by Month name

时光总嘲笑我的痴心妄想 提交于 2019-12-25 09:37:48
问题 I have a single function that takes 3 diffrent inputs: A dict of Days in the following formart: "Day 2":Something, "Day 1":Something, "Day 3":Something, "Day 5":Something, "Day 4":Something A dict of weeks: "Week 2":Something, "Week 1":Something, "Week 4":Something, "Week 3":Something, "Week 5":Something A dict of months: "April":Something, "February":Something, "March":Something, "January":Something Since they are dicts, they come in unordered. I'm manipulating them to present to the user in

Where can I find the algorithm used to write each PHP “built-in” function?

元气小坏坏 提交于 2019-12-25 04:20:03
问题 I recently built a PHP-based application that typically requires several (>10) seconds to parse a target string (>10 seconds because there are many thousands of checks on a typically 100kB+ string). I am looking for ways to reduce the execution time. I started to wonder how each of PHP's "built-in" functions are written. For example, if you go to the strpos() reference in the manual (this link), there is a lot of info but not the algorithm. Who knows, maybe I can write a function that is

Create a library for new built-ins Jena

匆匆过客 提交于 2019-12-25 02:22:14
问题 I have made some new built-ins for Jena. I would like to create a library where I can put all of them. How can I do that ? And how can I create my rules in this case ? Need I to import some files in the rule file? 回答1: Please note that, as this question is extremely broad, my answer is merely a set of suggestions towards an overall design. First, we'll begin with how Jena does it. Apache Jena stores its rule files as classpath resources within its distribution jars. jena-core has a package