473,387 Members | 2,436 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,387 software developers and data experts.

Accessing class attribute

Rafael Justo
Hi,

I'm new in python development (NEWBIE). While I was using Cheetah Templates I got a problem about accessing template variables.

I have an object like this (class Template):

Expand|Select|Wrap|Line Numbers
  1. >>> class A:
  2. ...  x = 1
  3. ...  y = 2
  4. ...  z = 3
  5.  
But I'm just going to know the attributes names "on the fly". How can I set/get this attributes if I have just a string with the name of the attribute?

I know that I can't do this:
Expand|Select|Wrap|Line Numbers
  1. >>> print A."x"
  2.   File "<stdin>", line 1
  3.     print A."x"
  4.               ^
  5. SyntaxError: invalid syntax
  6.  
  7. >>> A."y" = 4
  8.   File "<stdin>", line 1
  9.     A."y" = 4
  10.         ^
  11. SyntaxError: invalid syntax
  12.  
Any clues? =)

Best Regards!
Rafael
Jul 2 '09 #1
2 1792
bvdet
2,851 Expert Mod 2GB
Use built-in function getattr().

Expand|Select|Wrap|Line Numbers
  1. >>> class A(object):
  2. ...     x = 1
  3. ...     y = 2
  4. ...     z = 3
  5. ...     
  6. >>> getattr(A, 'x')
  7. 1
  8. >>> 
Jul 2 '09 #2
Thanks bvdet!

The setattr method was perfect for me!
setattr(class, attribute, value)
Jul 2 '09 #3

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

Similar topics

0
by: Robert Mark Bram | last post by:
Hi All! I have a menu system on my asp pages that use tables with a bunch of cells like this: <td class="menuHeader"><a href="../default.asp">Home</a></td> <td class="menuEntry"><a...
3
by: rockoyster | last post by:
Not sure if this is entirely a CSS issue but I have defined the class below in CSS. If I am stretching the friendship then I apologise in advance. I am trying to write a javascript script to...
1
by: Jiri Brada | last post by:
Hi, I have got a following problem with using Javascript: I have a HTML page with pictures' thumbnails. After clicking on any thumbnail, I would like to open a new window with the original...
3
by: Michael Iantosca | last post by:
I have a custom attribute that I attach to certain pages in my application and I want to inspect each page request as it is made to see if the custom attribute is attached to the underlying page...
9
by: Stacey | last post by:
I am using Visual Studio 2005, creating a VB.NET program. I am have created a settings file and two settings in the file. The section that it created in my app.config is below. I can not figure...
6
by: Gerard Flanagan | last post by:
Hello all I would like to do the following: from elementtree.SimpleXMLWriter import XMLWriter class HtmlWriter(XMLWriter, object): def write_raw(self, text): super( HtmlWriter, self...
14
by: James Thiele | last post by:
I'd like to access the name of a function from inside the function. My first idea didn't work. >>> def foo(): .... print func_name .... >>> foo() Traceback (most recent call last): File...
5
by: feli.hp | last post by:
Hello all, Imaybe someone can help me with this question. Is there a direct way of accessing an object instance, if all I know is the value of one of its attributes? The object is part of a...
10
by: Chris | last post by:
Hi, I'd like to be able to access an attribute of a particular Python object as fast as possible from some C code. I wondered if using __slots__ to store the attribute would allow me to do...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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
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,...
0
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...

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.