Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

So now you want to instantiate a variable of type "A", where "A" is a string that you read in from a configuration file. How do you do it in Python? (I guess it's possible somehow, I'm just wondering what the syntactic advantages are).

Your second sentence I don't understand - what do you mean 'variables whose type is a function'? The equivalent of a function pointer? For variable functions, you take a regular variable (i.e., with a $) and assign it the name of the function you want to call:

function call_me($arg) {} $func = 'call_me'; $func(1);



> So now you want to instantiate a variable of type "A", where "A" is a string that you read in from a configuration file. How do you do it in Python?

Depends on which kind of flexibility you want. You can use a dict of name to type i.e.

    d = {"A":A, "B":B}
    variable = d[string_from_file] (arguments)
or (perhaps slightly un-Pythonic, I guess)

    variable = getattr(my_types_module, string_from_file) (arguments)
where my_types_module is a module with your constructors.

The second version is somewhat closer to the PHP version, but still cleaner.

The problem with a system like

    function call_me($arg) {} $func = 'call_me'; $func(1);
is that it does not play well if you have several different functions of the same name but in different scopes. Or if you want to generate functions on the fly.

That's a concern, but not too much of a concern in a language like Python. I only chose Python because many people are somewhat familiar with it, and it does the Right Thing in this regard.

As an aside: Matlab recently changed from a PHP-like functions-passed-around-by-string-name system to functions-as-first-class-citizens system. They even had to introduce a new bit of syntax for that---but they found it important enough to go through with it. (Look at http://stackoverflow.com/questions/796935/function-handle-in... if you want to read about it.)


"variable = getattr(my_types_module, string_from_file) (arguments)"

Right, and how is that more readable or more clear than the PHP equivalent?

$myclass = new $classname; vs variable = getattr(my_types_module, string_from_file) (arguments) and you claim the second is cleaner?

Furthermore you are making several normative claims like 'Right Thing' but don't offer anything to back that opinion up. Function pointers are strongly typed too, does that make them superior? No just different, and I think the PHP way fits perfectly well with a consistent theme: you can substitute any name that you'd hard-code with a variable and it will work.

    $var = 'test';
    // Call function called 'test' with argument value of 'test';
    $var($var);
    // Set variable called '$test' to a new instance of class 'test'.
    $$var = new $var;
    // Call method 'test' on variable '$test' with argument 'test'.
    $test->{$test}($var);
Straightforward and no need for strange 'getattr' indirection functions (see how I turned an in itself neutral design decision into a normative statement there?)


Oh, I don't like getattr myself.

If you want, we can continue the discussion via email---it's getting a bit unwieldy for the comments here. My email is in my profile.

Thanks.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: