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

why is this so slow?

I created the following class (I know it's a dirty hack) so I could do
foo.bar instead of using a dictionary and having to type foo['bar'] :

class DefaultAttr(object):
def __getattribute__(self, attr):
if not hasattr(self, attr):
return ''
return object.__getattribute__(self,attr)

but its use is totally slowing down my program. Why is it so slow and is
there a better way?

Lowell
Jul 18 '05 #1
1 2794
Lowell Kirsh wrote:
I created the following class (I know it's a dirty hack) so I could do
foo.bar instead of using a dictionary and having to type foo['bar'] :

class DefaultAttr(object):
def __getattribute__(self, attr):
if not hasattr(self, attr):
return ''
return object.__getattribute__(self,attr)

but its use is totally slowing down my program. Why is it so slow and is
there a better way?


When you try foo.bar the __getattribute__ ends up
being called with self = foo and attr = "bar".

The hasattr(self, attr) is implemented something like

def hasattr(obj, attr):
try:
getattr(obj, attr)
return 1
except:
return 0

That is, hasattr will do the same thing that getattr
does. Which is exactly what foo.bar does. So you
have a recursive call here. To see that I'll
instrument the code you presented

count = 0

class DefaultAttr(object):
def __getattribute__(self, attr):
global count
if not hasattr(self, attr):
count += 1
return ''
return object.__getattribute__(self,attr)

After I defined the above I can test it like this
x = DefaultAttr()
print count 0 print x.y print count 500
See that count got set to 500? What happened was
the Python stack hit it's limit
import sys
sys.getrecursionlimit()

1000

Looks like there's one Python stack frame for
the hasattr and one for the __getattribute__.

You don't see the exception because the hasattr
assumes that *any* exception means that the
attribute doesn't exist. The actual code is in
dist/src/Python/bltinmodule.c in builtin_hasattr

v = PyObject_GetAttr(v, name);
if (v == NULL) {
PyErr_Clear();
Py_INCREF(Py_False);
return Py_False;
}
Py_DECREF(v);
Py_INCREF(Py_True);
return Py_True;

(Don't we tell people that a bare except is a no-no?
Eg, what if I hit ^C during the GetAttr test? Will
it be ignored? Or what if the process runs out of
memory? Perhaps this is related to "Thar the Windows
stack blows!" commentary in the python-dev summary
for 2004-08-01 to 08-15?)

In other words, all the time is spent in hitting
the stack limit.

You might try it the other way around and return the
underlying attribute then only if that fails do you
return the default, like this

count = 0
class DefaultAttr(object):
def __getattribute__(self, attr):
global count
count += 1
try:
return super(DefaultAttr, self).__getattribute__(attr)
except AttributeError:
return ""

x = DefaultAttr()
print "Start with", count
print "y is", repr(x.y)
print "And now", count
x.z = 5
print "Checking ...", count
print "z is", repr(x.z)
print "count after z", count
del x.z
print "z is now", repr(x.z)
print "count after 2nd z", count

When I run that I get

Start with 0
y is ''
And now 1
Checking ... 1
z is 5
count after z 2
z is now ''
count after 2nd z 3

Andrew
da***@dalkescientific.com
Jul 18 '05 #2

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

Similar topics

1
by: Jawahar Rajan | last post by:
All, I am using the ASP code below to save some data from a SQL Server database via ADO as an Excel spreadsheet strReportName = Request.QueryString("ReportName") If len(strReportName) > 0 then...
12
by: Aykut Canturk | last post by:
We perfectly develop our applicaitons with VS 6.0. We want to move to .net but all of our development computers almost die with a small test application. we have min P4 2ghz and 512MB desktop PCs...
3
by: Mario Soto | last post by:
Hi. i hava a postresql 7.4.2 in a production server. tha machine is a Pentium IV 2,6 GHZ AND 1 GB IN RAM with lINUX RH 9.0. The postresql.conf say: ...
50
by: diffuser78 | last post by:
I have just started to learn python. Some said that its slow. Can somebody pin point the issue. Thans
10
by: Extremest | last post by:
I know there are ways to make this a lot faster. Any newsreader does this in seconds. I don't know how they do it and I am very new to c#. If anyone knows a faster way please let me know. All...
5
by: Goran | last post by:
I have VS 2005. It works very slow.I have centrino 1,5 processor, 512 MB ram and hdd 4200 rpm. What I need to change or upgrade, ram or hdd or processor?
1
by: SamSpide | last post by:
Hi all, I have a moderate-side 'Windows Form Application' (C++) project, with several forms. For some reason switching between code & designer views (right-click ;view code' or 'view...
0
by: Pratchaya | last post by:
In my.cnf i add these lines ####### log-bin log-slow-queries = /var/log/mysqld-slow.log long_query_time=1 #######
2
by: mezise | last post by:
Posted by Pratchaya: ------------------------------------------------------ MySQL Slow Log ERROR In my.cnf i add these lines ####### log-bin log-slow-queries = /var/log/mysqld-slow.log
39
by: cm_gui | last post by:
Python is slow. Almost all of the web applications written in Python are slow. Zope/Plone is slow, sloow, so very slooow. Even Google Apps is not faster. Neither is Youtube. Facebook and...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
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
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
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...
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
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.