introspection

PHP callback: Is there an equivalent for ::class for a method of a class?

ぃ、小莉子 提交于 2021-02-20 02:52:15
问题 The bounty expires in 7 days . Answers to this question are eligible for a +50 reputation bounty. Blackbam wants to draw more attention to this question: A good solution shows a possibility to do this without passing a string but something which can be validated. If this is possible with two or three lines of codes using ReflectionClass this would totally be o.k. - I am just looking for a good way to solve this in a way such that I see an error in the IDE in case a method does not exist. In

Get package of Python object

ぃ、小莉子 提交于 2021-02-10 05:51:17
问题 Given an object or type I can get the object's module using the inspect package Example Here, given a function I get the module that contains that function: >>> inspect.getmodule(np.memmap) <module 'numpy.core.memmap' from ...> However what I really want is to get the top-level module that corresponds to the package, in this case numpy rather than numpy.core.memmap . >>> function_that_I_want(np.memmap) <module 'numpy' from ...> Given an object or a module, how do I get the top-level module?

How to find the name of a property from a Python class

 ̄綄美尐妖づ 提交于 2021-01-28 19:30:47
问题 I'm trying to get a property to print the name that it's assigned to in the owner class in python, and I've worked out a method that seems to work if and only if the property is directly assigned. It doesn't work if the property is inherited, as illustrated below. What's a good way so that when you call either Handler.CLIENT_ID or SubHandler.CLIENT_ID the variable name CLIENT_ID is always printed? class Auto(object): def __get__(self, instance, owner): attr_name = (k for (k, v) in owner._

Content checking some, not all, class attributes

纵然是瞬间 提交于 2021-01-27 03:57:55
问题 I have a class with attributes. I want to check whether some but not all are defined. So: class A { has $.a is rw; has $.b is rw; has $.c is rw; has $.d is rw; method delete { ... } } my A $x .= new(:a<hi>, :d<good>); ## later $x.b = 'there'; ## code in which $x.c may or may not be defined. ## now I want to check if the attributes a, b, and c are defined, without ## needing to know about d my Bool $taint = False; for <a b c> { $taint &&= $x.$_.defined } This will cause errors because an

Content checking some, not all, class attributes

百般思念 提交于 2021-01-27 03:54:21
问题 I have a class with attributes. I want to check whether some but not all are defined. So: class A { has $.a is rw; has $.b is rw; has $.c is rw; has $.d is rw; method delete { ... } } my A $x .= new(:a<hi>, :d<good>); ## later $x.b = 'there'; ## code in which $x.c may or may not be defined. ## now I want to check if the attributes a, b, and c are defined, without ## needing to know about d my Bool $taint = False; for <a b c> { $taint &&= $x.$_.defined } This will cause errors because an

What is the difference between introspection and reflection?

亡梦爱人 提交于 2020-12-27 07:38:32
问题 Can anyone explain from a language/environment agnostic perspective the difference between these two notions? Also is there a set of conditions that programming languages need to satisfy in order to be reflective and/or introspective? And if there is, what are these conditions? 回答1: The Wikipedia article has a pretty decent summary: In computing, type introspection is the ability of a program to examine the type or properties of an object at runtime. Some programming languages possess this

Getting a variable from the caller's globals. What is a frame object?

浪尽此生 提交于 2020-08-08 05:00:09
问题 The documentation for the inspect module says: When the following functions return “frame records,” each record is a named tuple FrameInfo(frame, filename, lineno, function, code_context, index) . The tuple contains the frame object, the filename, the line number of the current line, the function name, a list of lines of context from the source code, and the index of the current line within that list. What is actually a "frame object" ? I was hoping to use this frame object to get a variable

How to get struct field names in Rust? [duplicate]

安稳与你 提交于 2020-07-06 08:47:00
问题 This question already has answers here : Is there is a way to get the field names of a struct in a macro? (2 answers) Closed 4 years ago . Is there some equivalent of JS's Object.keys() for Rust's struct? I need something to generate CSV headers (I use rust-csv) from structure field names. struct Export { first_name: String, last_name: String, gender: String, date_of_birth: String, address: String } //... some code let mut wrtr = Writer::from_file("/home/me/export.csv").unwrap().delimiter(b'

How to find hidden properties/methods in Javascript objects?

℡╲_俬逩灬. 提交于 2020-06-26 09:42:41
问题 I would like to automatically determine all of the properties (including the hidden ones) in a given Javascript object, via a generalization of this function: function keys(obj) { var ll = []; for(var pp in obj) { ll.push(pp); } return ll; } This works for user defined objects but fails for many builtins: repl> keys({"a":10,"b":2}); // ["a","b"] repl> keys(Math) // returns nothing! Basically, I'd like to write equivalents of Python's dir() and help(), which are really useful in exploring new