built-in

Get all defined functions in Python module

情到浓时终转凉″ 提交于 2021-02-04 08:24:52
问题 I have a file my_module.py that looks like this: from copy import deepcopy from my_other_module import foo def bar(x): return deepcopy(x) I want to get a list of all the functions defined in my_module and not the imported ones, in this case just [bar] , not deepcopy or foo . 回答1: You can use inspect.getmembers with inspect.isfunction and then get all the functions whose .__module__ property is the same as the module's .__name__ : from inspect import getmembers, isfunction from my_project

Add a decorator to existing builtin class method in python

大兔子大兔子 提交于 2021-01-29 04:02:00
问题 I've got a class which contains a number of lists where whenever something is added to one of the lists, I need to trigger a change to the instance's state. I've created a simple demonstration class below to try to demonstrate what I'm trying to do. Suppose I have a class like this: class MyClass: added = False def _decorator(self, f): def func(item): added = true return f(item) return func def __init__(self): self.list = [1, 2, 3] self.list.append = self._decorator(self.list.append) Since a

How to check __builtin_ function is available on gcc

懵懂的女人 提交于 2021-01-27 05:21:14
问题 I need to know is there a method for gcc to check presence of those awesome __builtin_MY_DESIRED_FUNCTIONs For example, I'd like to use __builtin_nan and be sure it is available for my program and it won't fail during compilation time. I'll be more specific: on clang there is __has_builtin "checker" so we can write smth like #if __has_builtin(__builtin_nan) But I can't find analog for gcc. And probably I can rely just on gcc, like "Oh, I'm on gcc now, just let's assume all of those __builtin_

How to check __builtin_ function is available on gcc

非 Y 不嫁゛ 提交于 2021-01-27 05:20:50
问题 I need to know is there a method for gcc to check presence of those awesome __builtin_MY_DESIRED_FUNCTIONs For example, I'd like to use __builtin_nan and be sure it is available for my program and it won't fail during compilation time. I'll be more specific: on clang there is __has_builtin "checker" so we can write smth like #if __has_builtin(__builtin_nan) But I can't find analog for gcc. And probably I can rely just on gcc, like "Oh, I'm on gcc now, just let's assume all of those __builtin_

Trailing newlines and the bash 'read' builtin

和自甴很熟 提交于 2021-01-18 06:35:26
问题 In bash, this works: echo -n $'a\nb\nc\n' | while read x; do echo = $x =; done The while loops through three times = a = = b = = c = But imagine a text file that doesn't have the conventional trailing newline. I think that read should still work for all three lines, but it doesn't. I just get: echo -n $'a\nb\nc' | while read x; do echo = $x =; done = a = = b = The help read in bash doesn't really clarify. Note: I don't need this resolved, and I can see some ways to fix it myself. I am curious

Trailing newlines and the bash 'read' builtin

痴心易碎 提交于 2021-01-18 06:35:05
问题 In bash, this works: echo -n $'a\nb\nc\n' | while read x; do echo = $x =; done The while loops through three times = a = = b = = c = But imagine a text file that doesn't have the conventional trailing newline. I think that read should still work for all three lines, but it doesn't. I just get: echo -n $'a\nb\nc' | while read x; do echo = $x =; done = a = = b = The help read in bash doesn't really clarify. Note: I don't need this resolved, and I can see some ways to fix it myself. I am curious

How to hint to GCC that a line should be unreachable at compile time?

余生长醉 提交于 2020-12-25 01:55:10
问题 It's common for compilers to provide a switch to warn when code is unreachable. I've also seen macros for some libraries, that provide assertions for unreachable code. Is there a hint, such as through a pragma, or builtin that I can pass to GCC (or any other compilers for that matter), that will warn or error during compilation if it's determined that a line expected to be unreachable can actually be reached? Here's an example: if (!conf->devpath) { conf->devpath = arg; return 0; } // pass

How to hint to GCC that a line should be unreachable at compile time?

烂漫一生 提交于 2020-12-25 01:52:29
问题 It's common for compilers to provide a switch to warn when code is unreachable. I've also seen macros for some libraries, that provide assertions for unreachable code. Is there a hint, such as through a pragma, or builtin that I can pass to GCC (or any other compilers for that matter), that will warn or error during compilation if it's determined that a line expected to be unreachable can actually be reached? Here's an example: if (!conf->devpath) { conf->devpath = arg; return 0; } // pass

How to hint to GCC that a line should be unreachable at compile time?

╄→尐↘猪︶ㄣ 提交于 2020-12-25 01:50:49
问题 It's common for compilers to provide a switch to warn when code is unreachable. I've also seen macros for some libraries, that provide assertions for unreachable code. Is there a hint, such as through a pragma, or builtin that I can pass to GCC (or any other compilers for that matter), that will warn or error during compilation if it's determined that a line expected to be unreachable can actually be reached? Here's an example: if (!conf->devpath) { conf->devpath = arg; return 0; } // pass