dynamic-languages

Accessing an instance variable by name (string), kinda like dynamic languages do, in C#

╄→尐↘猪︶ㄣ 提交于 2021-02-04 06:51:50
问题 i've got some C# code like this: string fieldName = ... string value = ... if (fieldName == "a") a = value; if (fieldName == "b") b = value; if (fieldName == "c") c = value; if (fieldName == "d") d = value; ... I want something like this: string fieldName = ... string value = ... SetMyInstanceVariable(fieldName, value); ... Is there a simple way to do it? I know that given a class's name in a string, you can instantiate it with System.Activator, and this is kindof similar so i was hoping....

Any way to determine which object called a method?

故事扮演 提交于 2020-01-29 02:42:11
问题 I'm hoping that Ruby's message-passing infrastructure means there might be some clever trick for this. How do I determine the calling object -- which object called the method I'm currently in? 回答1: As an option, there is a binding_of_caller gem that allows you to execute code in context of any caller on the call stack (caller, caller's caller and so on). It's useful for inspecting (read do anything at any position on the call stack ) call stack in development, as used in better_errors.

How to access locals through stack trace? (Mimicking dynamic scope)

余生长醉 提交于 2020-01-01 05:05:09
问题 Background Even though it's possible to compile C# code at runtime, it's impossible to include and run the generated code in the current scope. Instead all variables have to be passed as explicit parameters. Compared with dynamic programming languages like Python, one could never truly replicate the complete behaviour of eval (as in this example). x = 42 print(eval("x + 1")) # Prints 43 The question So my question is (regardless if it's actually useful ;)) whether it's possible to mimic

Short description of the scoping rules?

独自空忆成欢 提交于 2019-12-31 05:28:25
问题 What exactly are the Python scoping rules? If I have some code: code1 class Foo: code2 def spam..... code3 for code4..: code5 x() Where is x found? Some possible choices include the list below: In the enclosing source file In the class namespace In the function definition In the for loop index variable Inside the for loop Also there is the context during execution, when the function spam is passed somewhere else. And maybe lambda functions pass a bit differently? There must be a simple

In what languages can you dynamically rewrite functions on the fly?

本小妞迷上赌 提交于 2019-12-23 10:47:11
问题 I recently had the necessity of rewriting a javascript function in javascript, dynamically. The ease with which I did it, and how fun it was, astounded me. Over here I've got some HTML: <div id="excelExport1234" onclick="if(somestuff) location.href='http://server/excelExport.aspx?id=56789&something=else'; else alert('not important');" >Click here to export to excel</div> And I couldn't change the outputted HTML, but I needed to add an extra parameter to that link. I started thinking about it,

Is DB connection pooling all that important?

别说谁变了你拦得住时间么 提交于 2019-12-21 22:04:24
问题 In the Java world it is pretty standard for app servers to pool "expensive" resources, like DB connections. On the other hand in dynamic languages, most stacks have little to do with pooled resources and especially DB connections. E.g. for the popular PHP+MySQL combo, I've rarely seen it used with persistent connection, which can be considered poor-mans pooled connections. If the concept of pooling DB connections is not that widely implemented, does this mean that the performance/scalability

How do you program differently in dynamic languages?

老子叫甜甜 提交于 2019-12-20 09:11:08
问题 How would someone who really knows how to take advantage of dynamic programming languages approach programming differently than someone working in a static language? I'm familiar with the whole debate over static versus dynamic typing, but that's not what I'm getting at. I'd like to discuss problem solving techniques that are practical in dynamic languages but not in static languages. Most of the code I've seen written in dynamic programming languages isn't very different than code written in

Uses for Dynamic Languages

喜欢而已 提交于 2019-12-18 15:54:15
问题 My primary language right now is D, and I'm in the process of learning Python because it's required for a course I'm taking. While I understand why dynamic languages would be a breath of fresh air for people programming in static languages without type inference or templates (IMHO templates are to a large extent compile-time duck typing), I'm curious what the benefits are of dynamic languages even when you have those. The bottom line is that, if I'm going to learn Python, I want to learn it

Can Perl be “statically” parsed?

别等时光非礼了梦想. 提交于 2019-12-18 12:28:38
问题 An article called "Perl cannot be parsed, a formal proof" is doing the rounds. So, does Perl decide the meaning of its parsed code at "run-time" or "compile-time"? In some discussions I've read, I get the impression the arguments stem from imprecise terminology, so please try to define your technical terms in your answer. I have deliberately not defined "run-time", "statically" or "parsed" so that I can get perspectives from people who perhaps define those terms differently to me. Edit: This

How to create inline objects with properties in Python?

試著忘記壹切 提交于 2019-12-18 10:58:23
问题 In Javascript it would be: var newObject = { 'propertyName' : 'propertyValue' }; How to do it in Python? 回答1: obj = type('obj', (object,), {'propertyName' : 'propertyValue'}) there are two kinds of type function uses. 回答2: Peter's answer obj = lambda: None obj.propertyName = 'propertyValue' 回答3: Python 3.3 added the SimpleNamespace class for that exact purpose: >>> from types import SimpleNamespace >>> obj = SimpleNamespace(propertyName='propertyValue') >>> obj namespace(propertyName=