Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > getting a class attribute using a keyword argument

Reply
Thread Tools

getting a class attribute using a keyword argument

 
 
Guy Robinson
Guest
Posts: n/a
 
      01-17-2005
Hello,

I have a list of class instances. I wish to get the appropriate class attribute
in each class instance depending on a SINGLE keyword in the calling class.

How do I get the calling method to correctly recognise the keyword as a keyword
and not a class attribute? See example code below (which doesn't work).

class tocall:
def __init__(self):
self.title = "test"
self.name = "name"

def callingmethod(self,**kw):
for key in kw:
if tocall.key == kw[key]:
return tocall.key

which should work as such(but doesn't):

print callmethod(title = "test")
print callmethod(name = "name")

Regards,

Guy
 
Reply With Quote
 
 
 
 
Nick Coghlan
Guest
Posts: n/a
 
      01-17-2005
Guy Robinson wrote:
> Hello,
>
> I have a list of class instances. I wish to get the appropriate class
> attribute in each class instance depending on a SINGLE keyword in the
> calling class.


Py> help(getattr)
Help on built-in function getattr in module __builtin__:

getattr(...)
getattr(object, name[, default]) -> value

Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.
When a default argument is given, it is returned when the attribute doesn't
exist; without it, an exception is raised in that case.

Cheers,
Nick.

--
Nick Coghlan | http://www.velocityreviews.com/forums/(E-Mail Removed) | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
 
Reply With Quote
 
 
 
 
wittempj@hotmail.com
Guest
Posts: n/a
 
      01-17-2005

Guy Robinson wrote:
> Hello,
>
> I have a list of class instances. I wish to get the appropriate class

attribute
> in each class instance depending on a SINGLE keyword in the calling

class.
>
> How do I get the calling method to correctly recognise the keyword as

a keyword
> and not a class attribute? See example code below (which doesn't

work).
>
> class tocall:
> def __init__(self):
> self.title = "test"
> self.name = "name"
>
> def callingmethod(self,**kw):
> for key in kw:
> if tocall.key == kw[key]:
> return tocall.key
>
> which should work as such(but doesn't):
>
> print callmethod(title = "test")
> print callmethod(name = "name")
>
> Regards,
>
> Guy

You probably want something like this:
class tocall:
def __init__(self):
self.title = "test"
self.name = "name"


x = tocall()

print getattr(x, 'title')
print getattr(x, 'name')
print getattr(x, 'bogus')

 
Reply With Quote
 
John Hsu
Guest
Posts: n/a
 
      01-19-2005
Guy Robinson wrote:

> Hello,
>
> I have a list of class instances. I wish to get the appropriate class
> attribute in each class instance depending on a SINGLE keyword in the
> calling class.
>
> How do I get the calling method to correctly recognise the keyword as a
> keyword and not a class attribute? See example code below (which doesn't
> work).
>
> class tocall:
> def __init__(self):
> self.title = "test"
> self.name = "name"
>
> def callingmethod(self,**kw):
> for key in kw:
> if tocall.key == kw[key]:
> return tocall.key
>
> which should work as such(but doesn't):
>
> print callmethod(title = "test")
> print callmethod(name = "name")
>
> Regards,
>
> Guy


Hi,

This may be more like you want.

class tocall:
def __init__(self):
self.title = "test"
self.name = "name"

def callmethod(**kw):
for key in kw:
if hasattr(tocall(), key):
return getattr(tocall(), key)

print callmethod(title = "test")
print callmethod(name = "name")

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ function argument with 'class' keyword? xhm C++ 8 01-25-2010 02:01 AM
RE: keyword checker - keyword.kwlist Hamilton, William Python 4 05-13-2007 06:31 AM
keyword checker - keyword.kwlist tom@finland.com Python 6 05-10-2007 04:53 PM
Passing a function as an argument and using the evaluated functionas an argument User1014 Javascript 1 11-30-2006 12:13 PM
a question on the new dict() keyword argument syntax Oktay Safak Python 2 08-02-2003 09:12 AM



Advertisments