473,386 Members | 1,699 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.

operator creation?

Hi all. Ill try to explain mi situation:
Lets say i have an DB object, who implements the querys to the database
trough a method called DBObject.doQuery.

On the other hand, i have 50 sql functions stored in the database. So i
can call DBObject.doQuery('select * from my_sql_function()')...Ok, what
do i want to achieve, is some mecanism to be able to call

DBObject.my_sql_function(), but without actually having to declare the
method called "my_sql_function". May be with creating a `->' operator...
maybe overrwriting the '.' operator...
Im not shure. If someone understand my problem (and my poor english),
please help :)

Gerardo

Mar 10 '06 #1
1 1052
Gerardo Herzig wrote:
Lets say i have an DB object, who implements the querys to the database
trough a method called DBObject.doQuery.

On the other hand, i have 50 sql functions stored in the database. So i
can call DBObject.doQuery('select * from my_sql_function()')...Ok, what
do i want to achieve, is some mecanism to be able to call

DBObject.my_sql_function(), but without actually having to declare the
method called "my_sql_function". May be with creating a `->' operator...
maybe overrwriting the '.' operator...


That's what __getattr__() is for. It is called when an attribute of the
DBObject instance doesn't exist. The function 'method' defined inside
__getattr__() remembers what the variable sql is bound to on that
particular call ("closure"). Because we update the class with 'method',
instead of __getattr__() subsequent calls will not my_sql_function()
directly.

class DBObject(object):
def doQuery(self, sql):
print "executing", sql
def __getattr__(self, name):
sql = "SELECT * FROM %s();" % name
def method(self):
return self.doQuery(sql)
# insert the new method into the class
# so that it has to be created only once
setattr(self.__class__, name, method)

return getattr(self, name) # infinite recursion
# prevented by prior setattr()

dbo = DBObject()
dbo.my_sql_function()
dbo.my_other_sql_function()

Peter
Mar 10 '06 #2

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

Similar topics

18
by: cppaddict | last post by:
Hi, Is it considered bad form to have the subscript operator return a const reference variable? If not, what is the proper way to do it? My question was prompted by the code below, my...
51
by: Jojo | last post by:
Is there any way to get to the left-hand side of an operator? Consider the following (this is not meant to be perfect code, just an example of the problem): class Matrix { public: int data;...
19
by: scroopy | last post by:
Is it impossible in C++ to create an assignment operator for classes with const data? I want to do something like this class MyClass { const int m_iValue; public: MyClass(int...
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
4
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Operator overloads are just like any other member function, you can make them do whatever you want. However, of course, we might expect them to behave in a certain way. The ++ operator should...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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
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,...

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.