473,770 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

conditionally creating functions within a class?

So, I have a class that has to retrieve some data from either xml or
an sql database.
This isn't a problem, but I was thinking "hey, it would be cool if I
could just not define the functions for say xml if I'm using sql", so
I did some fiddling around with the interpreter.

First, I try conditionally creating a function, period:

a = 'a'

if(a == 'a')
def b:
print "hello"
else:
def c:
print "goodbye"

this works fine. b is defined, c is not. change the value of a and b
gets defined and not c (sorry for the one-letter variables here, but
for these little examples I don't think they detract much)

then I try doing this within a function:

class test:
def __init__(self,w hich):
self.which = which

if(self.which == 'a'):
def b:
print "hello"
else:
def c:
print "goodbye"

tester = test('a')
tester.b()

This doesn't "compile", says "Name 'self' is not defined". I assume
this is because of scope, something like it hasn't made the object
yet, so there is no self attribute. . . but I thought that python
wouldn't even bother reading that class statement until I tried to
make a test object, and that it would do the __init__ function before
anything else, so I'm a bit fuzzy here.

Next I try creating the functions through functions:

class test:
def __init__(self, which):
self.which = which
self.chooser()

def chooser(self):
if( self.which == 'a'):
def b(self):
print "hello"
else:
def c(self):
print "goodbye"

tester = test('a')
tester.b()

this tells me "instance has no attribute b.

I'm pretty sure this is all a scoping error of some sort (I could be
wrong), but I don't have my head wrapped around it at all. Anyone with
more knowledge care to explain what's going on?

Also, would there be a way to conditionally create functions in a
class? It doesn't really matter, but it'd be nice if I weren't
creating functions that I absolutely will not need for certain
instances at runtime
May 26 '07 #1
1 5729
On May 25, 7:44 pm, kaens <apatheticagnos ...@gmail.comwr ote:
So, I have a class that has to retrieve some data from either xml or
an sql database.
This isn't a problem, but I was thinking "hey, it would be cool if I
could just not define the functions for say xml if I'm using sql", so
I did some fiddling around with the interpreter.

First, I try conditionally creating a function, period:

a = 'a'

if(a == 'a')
def b:
print "hello"
else:
def c:
print "goodbye"

this works fine. b is defined, c is not. change the value of a and b
gets defined and not c (sorry for the one-letter variables here, but
for these little examples I don't think they detract much)

then I try doing this within a function:

class test:
def __init__(self,w hich):
self.which = which

if(self.which == 'a'):
def b:
print "hello"
else:
def c:
print "goodbye"

tester = test('a')
tester.b()
...

class Test:
def __init__(self, which):
self.which = which
if which == 'a':
self.__class__ = TestB
else:
self.__class__ = TestC

class TestB(Test):
def b(self):
print "hello"

class TestC(Test):
def c(self):
print "goodbye"

May 26 '07 #2

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

Similar topics

7
3051
by: Harry Pehkonen | last post by:
I have been defining new class methods when I'm trying to simplify some code. But I'm thinking I should just define functions within that method because they aren't useful from the outside anyway. Example: Before: class Mess(object): def complicated(self, count):
4
2587
by: SainTiss | last post by:
Hi, From what I've read in several places, it seems that explicit specialization of member functions of class templates is allowed, but partial specialization isn't: template<class T, class R> class A { void foo(); }
7
1351
by: Brett Romero | last post by:
I need a static version of a class that can be referenced anywhere as a singleton and the same class that can be used as instances. Can this be done without basically creating the same class twice (one with static methods and one without)? Thanks, Brett
0
1067
by: Mike | last post by:
Hi. I have a vb.net/asp.net application, originally created in VS2003. I'm upgrading it to VS2005, and have found that Public Functions in a module are no longer accessable in my application. My code looks like this - and in this example I cannot call Function 123 within my application. --- Public Module ABC Public Function 123() as Boolean ...
6
9261
by: Mohammad Omer | last post by:
Hi, I tried to validate file path without calling file creating functions. Is it possible? How? Regards, -aims
3
1451
by: dgunjal | last post by:
Is there any method by which i can invoke C++ class functions within C functions?
7
8525
by: beginner | last post by:
Hi Everyone, I have encountered a small problems. How to call module functions inside class instance functions? For example, calling func1 in func2 resulted in a compiling error. "my module here" def func1(): print "hello"
2
2083
by: =?Utf-8?B?SnVsaWFu?= | last post by:
My first problem is: I am having an issue when using a web service. The web service is written by another department and say its running fine but when I call it, I sometimes I get a system.net.webexception return instead of the correct return. Would anyone have any advice or suggest any extra debugging I could do to find out where this problem might occur? The second problem I am having is: I also called some functions within the web...
1
2981
by: xuki | last post by:
Hi every one! I need your help to solve this problems. I'm trying to creat a wsdl file to a webservice but i'm getting an erros. "Error creating WSDL document:Error creating WSDL: no class found with the name 'array' / array : , so how should we know the structure for this datatype?" now i will show u the code and hope someone discover the bug! webservice.class.php
1
10008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9873
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8891
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7420
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3974
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
3
2822
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.