473,386 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Re: use str as variable name

2008/9/4 Chris Rebert <cv******@gmail.com>:
On Thu, Sep 4, 2008 at 12:25 AM, Mathieu Prevot
<ma************@gmail.comwrote:
>Hi,

I have a program that take a word as argument, and I would like to
link this word to a class variable.

eg.
class foo():

You should subclass 'object', so that should be:
class Foo(object):
> width = 10
height = 20

a=foo()
arg='height'
a.__argname__= new_value

You're looking for the setattr() built-in function. In this exact case:
setattr(a, arg, new_value)

This is probably covered in the Python tutorial, please read it.

Regards,
Chris
Indeed.

I'll use:
a.__setattr__(height, new_value)

Thanks to all
Mathieu
Sep 4 '08 #1
10 1642
Mathieu Prevot a écrit :
2008/9/4 Chris Rebert <cv******@gmail.com>:
(snip)
>You're looking for the setattr() built-in function. In this exact case:
setattr(a, arg, new_value)

This is probably covered in the Python tutorial, please read it.

Regards,
Chris

Indeed.

I'll use:
a.__setattr__(height, new_value)
Please don't. Use the generic setattr() function instead. This holds for
any __magic__ method : they are *implementation* for operators and
generic functions - which you can think of as operators with a function
syntax -, and are not meant to be called directly. You wouldn't write
something like 2.__add__(3), would you ?

Sep 4 '08 #2
Bruno Desthuilliers wrote:
You wouldn't write something like 2.__add__(3), would you ?
Don't give the "it's only OO if I write obj.method(args)" crowd more bad
ideas, please ;-)

(...as Bruno implies, setattr(), len() et al can be and should be viewed
as generic functions. A specific Python implementation may use custom
code to implement behaviour for a given object; behaviour that's more
efficient than a full Python-level method call. For example, in
CPython, len(L) is about twice as fast as L.__len__() for built-in
sequences.)

</F>

Sep 4 '08 #3
In article <48**********************@news.free.fr>,
Bruno Desthuilliers <br********************@websiteburo.invalidwrote :
>Mathieu Prevot a écrit :
>2008/9/4 Chris Rebert <cv******@gmail.com>:

(snip)
>>You're looking for the setattr() built-in function. In this exact case:
setattr(a, arg, new_value)

This is probably covered in the Python tutorial, please read it.

Regards,
Chris

Indeed.

I'll use:
a.__setattr__(height, new_value)

Please don't. Use the generic setattr() function instead. This holds for
any __magic__ method : they are *implementation* for operators and
generic functions - which you can think of as operators with a function
syntax -, and are not meant to be called directly. You wouldn't write
something like 2.__add__(3), would you ?
Along with the good advice the usual suspects have given,
my intuition is that there's an even better implementation
that doesn't setattr() at all. While it's impossible to
know, of course, because we don't have the original poster's
true requirements, I conjecture that, rather than "to link
this [user-supplied] word to a class variable", what will
serve him best is to regard the user text as an index into
a class dictionary.
Sep 4 '08 #4
2008/9/4 Fredrik Lundh <fr*****@pythonware.com>:
Bruno Desthuilliers wrote:
>You wouldn't write something like 2.__add__(3), would you ?

Don't give the "it's only OO if I write obj.method(args)" crowd more bad
ideas, please ;-)

(...as Bruno implies, setattr(), len() et al can be and should be viewed as
generic functions. A specific Python implementation may use custom code to
implement behaviour for a given object; behaviour that's more efficient than
a full Python-level method call. For example, in CPython, len(L) is about
twice as fast as L.__len__() for built-in sequences.)
Got it. Thanks :)
Mathieu
Sep 4 '08 #5
On Thu, Sep 4, 2008 at 10:47 AM, Fredrik Lundh <fr*****@pythonware.comwrote:
>
(...as Bruno implies, setattr(), len() et al can be and should be viewed as
generic functions.
Just a question: "generic functions" are not meant in the sense of
"generic functions" of CLOS, am I right?

--
Marco Bizzarri
http://notenotturne.blogspot.com/
http://iliveinpisa.blogspot.com/
Sep 5 '08 #6
Marco Bizzarri wrote:
>(...as Bruno implies, setattr(), len() et al can be and should be viewed as
generic functions.

Just a question: "generic functions" are not meant in the sense of
"generic functions" of CLOS, am I right?
it's meant in exactly that sense: len(L) means "of all len()
implementations available to the runtime, execute the most specific code
we have for the object L".

</F>

Sep 6 '08 #7
On Fri, Sep 5, 2008 at 9:16 PM, Bruno Desthuilliers
<bd*****************@free.quelquepart.frwrote:
Marco Bizzarri a écrit :
>>
Just a question: "generic functions" are not meant in the sense of
"generic functions" of CLOS, am I right?

Nope. Just "generic" in the sense that they accept any object implementing a
very minimal interface.

If you want something like CLOS multimethods, you may be interested in
Philip Eby's ruledispatch.
Even though I loved them when I used at university, I'm not looking
for them right now... but nice to know that they are available under
python :-)
--
Marco Bizzarri
http://notenotturne.blogspot.com/
http://iliveinpisa.blogspot.com/
Sep 6 '08 #8
On Sat, Sep 6, 2008 at 7:52 AM, Fredrik Lundh <fr*****@pythonware.comwrote:
Marco Bizzarri wrote:
>>(...as Bruno implies, setattr(), len() et al can be and should be viewed
as
generic functions.

Just a question: "generic functions" are not meant in the sense of
"generic functions" of CLOS, am I right?

it's meant in exactly that sense: len(L) means "of all len() implementations
available to the runtime, execute the most specific code we have for the
object L".
It is a generic functions like a CLOS one, as long as we remain to one
parameter.

I mean, there will be just one implemenatation of

foo(bar, man)

which the python interpretr can find; am I right?

--
Marco Bizzarri
http://notenotturne.blogspot.com/
http://iliveinpisa.blogspot.com/
Sep 6 '08 #9
On Sep 6, 8:02*am, "Marco Bizzarri" <marco.bizza...@gmail.comwrote:
On Fri, Sep 5, 2008 at 9:16 PM, Bruno Desthuilliers

<bdesth.quelquech...@free.quelquepart.frwrote:
Marco Bizzarri a écrit :
Just a question: "generic functions" are not meant in the sense of
"generic functions" of CLOS, am I right?
Nope. Just "generic" in the sense that they accept any object implementing a
very minimal interface.
If you want something like CLOS multimethods, you may be interested in
Philip Eby's ruledispatch.

Even though I loved them when I used at university, I'm not looking
for them right now... but nice to know that they are available under
python :-)
Actually they are already available in the standard library but they
are undocumented. See for instance
this recent blog post of mine:

http://www.artima.com/weblogs/viewpo...?thread=237764

(as well as the comment below by P.J. Eby)
Sep 6 '08 #10
Marco Bizzarri wrote:
>>Just a question: "generic functions" are not meant in the sense of
"generic functions" of CLOS, am I right?

it's meant in exactly that sense: len(L) means "of all len() implementations
available to the runtime, execute the most specific code we have for the
object L".

It is a generic functions like a CLOS one, as long as we remain to one
parameter.

I mean, there will be just one implemenatation of

foo(bar, man)

which the python interpretr can find; am I right?
Let's see if I can sort this out without causing even more confusion.

The Python *language* doesn't support generic functions in the CLOS
sense, but a given Python *implementation* may use a dispatching
machinery to select the best possible implementation for any call to a
built-in function.

Or in other words, the len() function shouldn't just be seen as a
function that *always* does

def len(L):
return L.__len__()

because if you look under the covers, it might be more like (using a
hypothetical Python dialect):

def generic len(L: list):
return list::get_size(L) # fast internal dispatch

def generic len(L: tuple):
return tuple::get_size(L) # fast internal dispatch

def generic len(L: object):
return L.__len__() # fallback behaviour, using method dispatch

where "len" represents a plurality of possible "len" implementations.

How the dispatching is actually done is up to the specific Python
implementation; CPython, for example, offers a "slot" mechanism for
types implemented in C that's quite a bit faster than the method call
machinery. And the slot mechanism isn't always single dispatch. For
example, internal getattr(obj, name) calls use one of several slots,
depending on what "name" is.

Other Python implementations may use different approaches, but the point
remains: a builtin function "operation(a, b, c)" isn't always mapped to
"a.__operation__(b, c)" by the runtime; code that uses the latter form
may be less efficient. And it's definitely less Pythonic.

</F>

Sep 6 '08 #11

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

Similar topics

1
by: Scott | last post by:
I have an XML Document in a format like: <Variable name="Bob">ABCDEFG</Variable> <Variable name="Steve">QWERTYUI</Variable> <Variable name="John">POIUYTR</Variable> <Variable...
2
by: Bradford | last post by:
Question for the masses... Lets say I have variable with the following contents "aaaa bbbb ccccc dddd". The format is not specific and the space delimiter could be changed to any other. How...
4
by: Frederik Sørensen | last post by:
I include a xslt stylesheet with variables for all the error messages in my system. <xsl:variable name="Banner_error_1"> errormessage 1 for banner </xsl:variable> <xsl:variable...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
3
by: rls03 | last post by:
I have the following which creates a variable containing a relative path where <xsl:value-of select="."/returns a portion of the filename: <xsl:variable...
2
by: Kevin | last post by:
I am having difficulty updating a variable page-time-stamp in the following snippit. The variable time-stamp is initialized from the attribute time-stamp from the log element. Some of the page...
2
by: Looch | last post by:
All, I'm trying to output but I can only get (brackets for clarity) when using the code below. How can I "break" into the query variable in the InsertName method to add the name parameter to...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.