473,321 Members | 1,708 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,321 software developers and data experts.

Using getattr in Python

Just a basic question .... When using getattr for a specific method from a different class, can we somehow tell getattr to do a case-insensitive search.

Say for eg. m = getattr(obj.__class__,'SetupClass') where obj is a object of a specific class.

If SetupClass is declared as setUPClass or in any other way, the return value of m is going to be None, so can this be avoided in any way

Thanks
Feb 9 '07 #1
2 5505
bartonc
6,596 Expert 4TB
Just a basic question .... When using getattr for a specific method from a different class, can we somehow tell getattr to do a case-insensitive search.

Say for eg. m = getattr(obj.__class__,'SetupClass') where obj is a object of a specific class.

If SetupClass is declared as setUPClass or in any other way, the return value of m is going to be None, so can this be avoided in any way

Thanks
In your class, you can play with the names any way that you want to as I do here. In my default holder class "default" is appended inside the class so that I can pick out only that attrs that I want and stripped of on the way out. Have fun and keep posting,
Barton
Feb 9 '07 #2
bvdet
2,851 Expert Mod 2GB
Just a basic question .... When using getattr for a specific method from a different class, can we somehow tell getattr to do a case-insensitive search.

Say for eg. m = getattr(obj.__class__,'SetupClass') where obj is a object of a specific class.

If SetupClass is declared as setUPClass or in any other way, the return value of m is going to be None, so can this be avoided in any way

Thanks
In this example 'name' is a class attribute of instance d2.__class__, but could have been a method as well:
Expand|Select|Wrap|Line Numbers
  1. >>> s = 'NaMe'
  2. >>> def lower_name(s, obj):
  3. ...     for item in dir(obj):
  4. ...         if item.lower() == s.lower():
  5. ...             return item
  6. ...         
  7. >>> lower_name(s, d2)
  8. 'name'
  9. >>> getattr(d2.__class__, lower_name(s, d2))
  10. 'Plane3D'
  11. >>> d2.name
  12. 'Plane3D'
  13. >>> 
Feb 9 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: daishi | last post by:
Hi, The following code appears to be doing what I'd expect, but I'm wondering if someone could confirm that there aren't any "gotchas" hidden in using methods accessed in this way. In...
8
by: Steven D'Aprano | last post by:
I came across this unexpected behaviour of getattr for new style classes. Example: >>> class Parrot(object): .... thing = .... >>> getattr(Parrot, "thing") is Parrot.thing True >>>...
13
by: Pierre | last post by:
Hi, Sorry in advance, english is not my main language :/ I'd like to customize the result obtained by getattr on an object : if the object has the requested property then return it BUT if the...
12
by: creo | last post by:
Hi all! this is a (relatively) newbie question I am writing a shell in Python and I am facing a problem The problem is, after taking the input from user, i have to execute the command...
3
by: pranav.choudhary | last post by:
Hi, AIM: I have a config file that contains configuration under headings like this: heading1: <configuration 1> <configuration 2> heading2: <configuration 1>
3
by: Jm lists | last post by:
Hello, Since I can write the statement like: Test whether a path is a directory Why do I still need the getattr() func as below? Test whether a path is a directory
3
by: Michal Lipinski | last post by:
Hi its my first post. I have a problem, I want to user eval() function in a for loop to set labels to staticText so i done something like this: ...
2
by: Sledge | last post by:
Hi. I am trying to dynamically load a class and attributes at run time. I do not know what classes will be referenced until run time. I have it loading the module correctly, but when I use...
8
by: Gregor Horvath | last post by:
Hi, class A(object): test = "test" class B(object): a = A() In : B.a.test
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.