473,574 Members | 2,291 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getting a class attribute using a keyword argument

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(s elf,**kw):
for key in kw:
if tocall.key == kw[key]:
return tocall.key

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

print callmethod(titl e = "test")
print callmethod(name = "name")

Regards,

Guy
Jul 18 '05 #1
3 1506
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 | nc******@email. com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #2

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(s elf,**kw):
for key in kw:
if tocall.key == kw[key]:
return tocall.key

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

print callmethod(titl e = "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')

Jul 18 '05 #3
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(s elf,**kw):
for key in kw:
if tocall.key == kw[key]:
return tocall.key

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

print callmethod(titl e = "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(titl e = "test")
print callmethod(name = "name")

Jul 18 '05 #4

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

Similar topics

37
12710
by: ajay | last post by:
How to make a web page getting refreshed after a given time interval automatically. HTML Code plz. Tx Ajay
4
1827
by: venk | last post by:
Hi, given below is my interaction with the interpreter.... In one case, i have created the class method using the "famous idiom"... and in the other, i have tried to create it outside the class definition... why isn't the latter working ? (of course, the presence of decorators is a different issue).... >>> class D: .... def f(cls):...
0
3019
by: keith bannister via .NET 247 | last post by:
(Type your message here) -------------------------------- From: keith bannister Hi, I'm new to .net (as of last week) but here goes. I want to serialize/deserialize a file the conforms to an XML schema (xsd).
5
8724
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but conceptually speaking : when do I define something as 'struct' and when as 'class' ? for example : if I want to represent a 'Time' thing, containing...
5
6846
by: Dylan Moreland | last post by:
I'm trying to implement a bunch of class methods in an ORM object in order to provide functionality similar to Rails' ActiveRecord. This means that if I have an SQL table mapped to the class "Person" with columns name, city, and email, I can have class methods such as: Person.find_by_name Person.find_by_city_and_name...
14
5410
by: Jamey Shuemaker | last post by:
Greetings all, I've been reading for the last couple hours posts on this site and various MS sites about reference libraries and class modules. System: Windows 2K running an A2K db with various standard modules and a handful of custom class modules. Stripped out the standard and class modules and put them in a stand-alone mdb which was...
1
2307
by: petschy | last post by:
hello, i've run into an error when qualifying a copy ctor 'explicit'. the strange thing is that i get a compiler error only if the class is a template and declare the variable as X<Zx = y. X<Zx(y) is fine. Tested with gcc 2.95, 3.3, 4.1, all gave the same error: t.cpp: In function 'int main()': t.cpp:44: error: no matching function for...
9
1589
by: 7stud | last post by:
Hi, I'm trying to figure out what this passage from GvR's tutorial means: ------- Class definitions, like function definitions (def statements) must be executed before they have any effect.... When a class definition is entered, a new namespace is created... When a class definition is left normally (via the end), a class object
13
2721
by: Hussein B | last post by:
Hi, I'm familiar with static method concept, but what is the class method? how it does differ from static method? when to use it? -- class M: def method(cls, x): pass method = classmethod(method) --
0
7801
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7719
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8229
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8096
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6450
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5616
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3749
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2238
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 we have to send another system
1
1335
muto222
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.