built-in

Python sum, why not strings? [closed]

杀马特。学长 韩版系。学妹 提交于 2019-12-24 19:13:39
问题 Closed . This question is opinion-based. It is not currently accepting answers. Closed last year . Locked . This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions. Python has a built in function sum , which is effectively equivalent to: def sum2(iterable, start=0): return start + reduce(operator.add, iterable) for all types of parameters except strings. It works for numbers and lists

Override __repr__ or pprint for int

笑着哭i 提交于 2019-12-24 08:30:15
问题 Is there any way of changing the way a int -type object is converted to string when calling repr or pprint.pformat , such that repr(dict(a=5, b=100)) will give "{a: 0x5, b: 0x64}" instead of "{a: 5, b: 100}" ? I suppose subclassing the int type would be an option: class IntThatPrintsAsHex(int): def __repr__(self): return hex(self) def preprocess_for_repr(obj): if isinstance(obj, dict): return {preprocess_for_repr(k): preprocess_for_repr(v) for k, v in obj.items()} elif isinstance(obj, list):

How to use a built-in function if its name is used by another module?

隐身守侯 提交于 2019-12-24 05:24:12
问题 For example, there is a built-in function any in Python. The problem is, when the module numpy is imported, the definition of function any is changed. How can I used the original function any in the __builtin__ module? For example: from numpy import * any(i % 3 for i in [3, 3, 4, 4, 3]) and the code will not work! Sorry I am a newbie in Python. 回答1: You can still reach the object on the __builtin__ module: import __builtin__ __builtin__.any(i % 3 for i in [3, 3, 4, 4, 3]) (The module was

Is there a way to change the default eclipse toolbar icons built into eclipse?

£可爱£侵袭症+ 提交于 2019-12-24 05:21:53
问题 I am talking about the tiny icons on the toolbar. There doesn't seem to be any questions like this on the web, they all refer to android or a custom application as opposed to the icons bundled with eclipse. I want to know if anyone has tried this or can tell me that its not worth my time because it is a lot of work. Cheers. 回答1: I don't think this is possible. The standard icons are declared using the org.eclipse.ui.commandImages extension point in the org.eclipse.ui plugin and reference

Is there a way to change the default eclipse toolbar icons built into eclipse?

[亡魂溺海] 提交于 2019-12-24 05:21:07
问题 I am talking about the tiny icons on the toolbar. There doesn't seem to be any questions like this on the web, they all refer to android or a custom application as opposed to the icons bundled with eclipse. I want to know if anyone has tried this or can tell me that its not worth my time because it is a lot of work. Cheers. 回答1: I don't think this is possible. The standard icons are declared using the org.eclipse.ui.commandImages extension point in the org.eclipse.ui plugin and reference

How to check variable type using “type” function in Sikuli

风格不统一 提交于 2019-12-24 00:58:27
问题 Sikuli has its own function type for typing. Is there any way to invoke python (jython) function type ? Module builtins can’t be imported. Of course I can use isinstance instead but I am just curious if it is possible to come outside Sikuli scope and invoke not “overridden”, python built-in type function. I use Sikuli r930. #import builtins #ImportError: No module named builtins findAll("1369036502514.png") matches = getLastMatches() print(isinstance(matches,Finder)) #returns TRUE type(

two conflicting meanings of builtins in python 3 (python 3.1, python 3k, python3000)

折月煮酒 提交于 2019-12-23 16:47:12
问题 I just posted below query to comp.lang.python, but i feel this kind of question has some kind of right-of-way here on Stack Overflow, too, so be it repeated. the essence: why does ‘builtins’ have two distinct interpretations in Python 3? I would be very gladly accept any commentaries about what this sentence, gleaned from http://celabs.com/python-3.1/reference/executionmodel.html, is meant to mean, or why gods have decided this is the way to go. i anticipate this guy named Kay Schluehr will

zip()-like built-in function filling unequal lengths from left with None value

我与影子孤独终老i 提交于 2019-12-22 08:52:56
问题 Is there a built-in function that works like zip(), but fills the results so that the length of the resulting list is the length of the longest input and fills the list from the left with e.g. None ? There is already an answer using zip_longest from itertools module and the corresponding question is very similar to this. But with zip_longest it seems that you can only fill missing data from the right. Here might be a use case for that, assuming we have names stored only like this (it's just

Python inverse function of id(…) built-in function

倾然丶 夕夏残阳落幕 提交于 2019-12-21 04:49:11
问题 Is there a reverse or inverse of the id built-in function? I was thinking of using it to encode and decode string without taking too much time or having a lot of overhead like the PyCrypto library. The need for me is quite simple so I don't want to use PyCrypto for a simple encode and decode. Something like: >>> id("foobar") 4330174256 >>> reverse_id(4330174256) # some function like this to reverse. "foobar" 回答1: I do not wanna to steal the credits from the man who answered the question This

Why I can call 'print' from 'eval'

眉间皱痕 提交于 2019-12-20 14:12:35
问题 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