473,471 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Referencing vars, methods and classes by name

Greetings,

Sorry for a newbiw question: what is a most elegant and/or effective
way to reference vars, methods and classes by their names in Python?

To illustrate, PHP code:

$a = ''b';
$$a = $something; // assign to $b
$$a($p1); // call function b($p1)
$obj->$a(); // call method b() of the instance $obj

What is the Python way of performing the same indirections?

References to online docs/articles related to the subject are very
welcome.

Thank you!

Konstantin

Feb 8 '07 #1
9 1059
Quite forgot to add the obvious example (in PHP):

$a = 'b';
$obj =& new $a(); // instantiating class b()

Feb 8 '07 #2
"Sagari" <bo******@gmail.comwrites:
$a = ''b';
$$a = $something; // assign to $b
$$a($p1); // call function b($p1)
$obj->$a(); // call method b() of the instance $obj

What is the Python way of performing the same indirections?
We would not do that. We don't (usually) use the interpreter symbol
table as a dictionary (what in perl would be called a hash). Instead
we use an actual dictionary. We might say

d = {} # make an empty dictionary
a = 'b'
d[a] = something # assign to d['b']
some_functab[a](p1) # look up 'b' in some function table and call it

For your last example we could say

obj.getattr(a)()

but even that is a bit ugly, depending.

For your other examples there are gross hacks using the dictionaries
that represent the local and global symbol tables, so we translate
your examples fairly directly, but stylistically we'd usually stay
away from that kind of thing.
Feb 8 '07 #3
"Sagari" <bo******@gmail.comwrites:
$a = 'b';
$obj =& new $a(); // instantiating class b()
Classes are first class objects in python:

class b: ..... # define class b

We could assign the class object to a variable

a = b

and make an instance:

obj = a() # same as "obj = b()"

Continuing that example we could make a dispatch table of classes:

class b: ...
class c: ...
class d: ...

table = [b, c, d] # 3 element array, each element is a class

i = 1
a = table[i] # this means a = c

obj = a() # instantiate c
Feb 8 '07 #4
En Thu, 08 Feb 2007 05:29:23 -0300, Paul Rubin
<"http://phr.cx"@NOSPAM.invalidescribió:
"Sagari" <bo******@gmail.comwrites:
>$a = ''b';
$obj->$a(); // call method b() of the instance $obj

What is the Python way of performing the same indirections?

For your last example we could say

obj.getattr(a)()

but even that is a bit ugly, depending.
Surely you meant to say getattr(obj, a)()

--
Gabriel Genellina

Feb 8 '07 #5
"Gabriel Genellina" <ga******@yahoo.com.arwrites:
obj.getattr(a)()
but even that is a bit ugly, depending.
Surely you meant to say getattr(obj, a)()
Yeah, darn. Counterintuitive. I keep making that error in my own
code too. Maybe I should put in an RFE.
Feb 8 '07 #6
On 8 feb, 05:51, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
"Gabriel Genellina" <gagsl...@yahoo.com.arwrites:
obj.getattr(a)()
but even that is a bit ugly, depending.
Surely you meant to say getattr(obj, a)()

Yeah, darn. Counterintuitive. I keep making that error in my own
code too. Maybe I should put in an RFE.
The "method" way is using __getattribute__ or __getattr__.
A generic function helps on using it on objects of any kind - like
len()
Perhaps it was more important with old style classes.

--
Gabriel Genellina

Feb 8 '07 #7
Gabriel Genellina wrote:
On 8 feb, 05:51, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
"Gabriel Genellina" <gagsl...@yahoo.com.arwrites:
Surely you meant to say getattr(obj, a)()
Yeah, darn. Counterintuitive.

A generic function helps on using it on objects of any kind - like
len()
Perhaps it was more important with old style classes.
It also avoids intruding on the method namespace of the
object. That's important -- I like the way that the
namespace of a brand-new class is a blank slate, apart
from the double-underscore names.

--
Greg
Feb 9 '07 #8
For your other examples there are gross hacks using the dictionaries
that represent the local and global symbol tables, so we translate
your examples fairly directly, but stylistically we'd usually stay
away from that kind of thing.
Thanks to everyone for all the comments. I am migrating from PHP to
Python and I am looking for the means to port a controller code that
would, roughly speaking, call a certain method of a certain class
(both class and method names taken from user input). Putting aside
input verification (by the moment I perform the call input must have
been verified), what would be the recommended way of doing the trick?

Thanks!

All the best,

Konstantin

Feb 9 '07 #9

"Sagari" <bo******@gmail.comwrote in message
news:11**********************@p10g2000cwp.googlegr oups.com...
| Thanks to everyone for all the comments. I am migrating from PHP to
| Python and I am looking for the means to port a controller code that
| would, roughly speaking, call a certain method of a certain class
| (both class and method names taken from user input). Putting aside
| input verification (by the moment I perform the call input must have
| been verified), what would be the recommended way of doing the trick?

Use getattr(ob, name).
Suppose
mod = <module with classes, imported as 'mod'>
cname = <class name from user>
mname = <method name from user>

Then
classobj = getattr(mod, cname)
methobj = getattr(classobj, mname)

However: Python generally uses methods for instance methods. Would you be
creating an instance of the class (easily done -- inst = classobj(args))
before calling the method? If so, call methodobj(inst, args). If not, I
wonder I you should really be using (Python) classes.

Terry Jan Reedy

Feb 9 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: xcomm | last post by:
Hi All, <?php $vars= array("_SERVER","_SERVER","_SERVER","_SERVER","_SERVER","_SERVER"); foreach($vars as $var) { if(isset($$var))echo("$var: ${$var}<br>\n"); } ?> php.net:
99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
6
by: flamesrock | last post by:
ok, so to my knowledge, object oriented means splitting something into the simplest number of parts and going from there. But the question is- when is it enough? For example I have the following...
7
by: Jim Adamson | last post by:
I have created a web page that receives names and values from a URL string of another page e.g. http://hostname/resolve?sublibrary=JMLibrary&collection=Elton&shelfmark=LM 36TY ... and decodes the...
10
by: Macka | last post by:
A few pieces of information first: * I have a class called Folder which represents a row of data in a database table. The data access side of things is not an issue. * The table has a parent...
12
by: Mark Broadbent | last post by:
Hi guys, just going through remoting at the moment and a couple of questions relating to .net in general has surfaced. Firstly I have seen in the designer that for the namespace and many of its...
4
by: Keith Chadwick | last post by:
I am having some trouble referencing an Application("myVar") variable from within a module.vb file on my ASP.NET site. According to the documentation I should be able to reference...
4
by: PaowZ | last post by:
Hello there! I'd like to make some declared vars available to classes, such as: <? //main environment $myVar = new MyClass(); //the var I want to be available to OtherClass .... .... class...
2
by: Andrus | last post by:
I need compile in-memory assembly which references to other in-memory assembly. Compiling second assembly fails with error Line: 0 - Metadata file 'eed7li9m, Version=0.0.0.0, Culture=neutral,...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.