I'm trying to find the preferred python idiom for access arbitrary
fields of objects at run time.
For example, say I have an object the business code will do
*something* with three arbitrary fields at a given time, but I don't
know what the three fields are at run time. In perl, I'd do something
like this:
sub three_fields{
my $self = shift;
my @fields = @_;
foreach my $field (@fields)
{
my $value = $self->$field; # this is the one I'm
interested in
[...]
}
}
In python, I'm doing something like this:
def three_fields(self, field1, field2, field3):
for field in (field1, field2, field3):
value = eval('self.' + field) # this is the one I'm
interested in
[...]
This seems to do what I expect it to do. I'm wondering if that's the
preferred or standard way to do this in python. I wasn't sure how to
tease the answer to this question out of Google.
Thanks. 2 1048
mshiltonj wrote:
In python, I'm doing something like this:
def three_fields(self, field1, field2, field3):
for field in (field1, field2, field3):
value = eval('self.' + field) # this is the one I'm
interested in
[...]
This seems to do what I expect it to do. I'm wondering if that's the
preferred or standard way to do this in python. I wasn't sure how to
tease the answer to this question out of Google.
I believe you are looking for the getattr function.
--
--OKB (not okblacke)
Brendan Barnwell
"Do not follow where the path may lead. Go, instead, where there is
no path, and leave a trail."
--author unknown
On Jul 8, 2:31 pm, "OKB (not okblacke)"
<brenNOSPAMb...@NObrenSPAMbarn.netwrote:
mshiltonj wrote:
In python, I'm doing something like this:
def three_fields(self, field1, field2, field3):
for field in (field1, field2, field3):
value = eval('self.' + field) # this is the one I'm
interested in
[...]
This seems to do what I expect it to do. I'm wondering if that's the
preferred or standard way to do this in python. I wasn't sure how to
tease the answer to this question out of Google.
I believe you are looking for the getattr function.
--
--OKB (not okblacke)
Brendan Barnwell
"Do not follow where the path may lead. Go, instead, where there is
no path, and leave a trail."
--author unknown
Doh. I figured it'd by FAQish. :-/ Thanks. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Stephen C. Waterbury |
last post by:
This seems like it ought to work, according to the
description of reduce(), but it doesn't. Is this
a bug, or am I missing something?
Python 2.3.2 (#1, Oct 20 2003, 01:04:35)
on linux2
Type...
|
by: Olivier Scalbert |
last post by:
Hello ,
What is the python way of doing this :
perl -pi -e 's/string1/string2/' file
?
Thanks
Olivier
|
by: David Morgenthaler |
last post by:
In many of my scripts I've used the following idiom for accessing data
files placed nearby:
BASEDIR = os.path.dirname(__file__)
..
..
..
fp = file(os.path.join(BASEDIR,"somefile.txt"))
..
..
|
by: Simon Wittber |
last post by:
For the first time, I have been bitten by Python. The below code
produces the results:
False
True
when I initially expected the results:
False
False
It took me a while to work out that...
|
by: Frank Samuelson |
last post by:
I love Python, and it is one of my 2 favorite
languages. I would suggest that Python steal some
aspects of the S language.
-------------------------------------------------------
1. Currently...
|
by: Andrew Savige |
last post by:
I'm learning Python by reading David Beazley's "Python Essential Reference"
book and writing a few toy programs. To get a feel for hashes and sorting,
I set myself this little problem today (not...
|
by: Jordan |
last post by:
Hi everyone,
I'm a big Python fan who used to be involved semi regularly in
comp.lang.python (lots of lurking, occasional posting) but kind of
trailed off a bit. I just wrote a frustration...
|
by: Ivan Reborin |
last post by:
Hello everyone,
I was wondering if anyone here has a moment of time to help me with 2
things that have been bugging me.
1. Multi dimensional arrays - how do you load them in python
For...
|
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...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |