471,073 Members | 1,385 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,073 software developers and data experts.

def obj()

def obj():
return {'data':'hello',
'add':add(v)}

def add(v):
data=data+v

if __name__ == '__main__':
test=obj()
test.add('world')
print test.data

I don't know why but i have one of does none class c programing style
moods again. I was wondering if the following was possible without
using a class ?
Feb 8 '07 #1
3 1015
Gert Cuykens wrote:
def obj():
return {'data':'hello',
'add':add(v)}

def add(v):
data=data+v

if __name__ == '__main__':
test=obj()
test.add('world')
print test.data

I don't know why but i have one of does none class c programing style
moods again. I was wondering if the following was possible without
using a class ?
def obj():
result = {'data': 'hello'}
result['add'] = adder(result)
return result

def adder(obj):
def add(value):
obj['data'] += value
return add

if __name__ == '__main__':
test = obj()
test['add']('world')
print test['data']
Feb 8 '07 #2
On 2/8/07, Leif K-Brooks <eu*****@ecritters.bizwrote:
def obj():
result = {'data': 'hello'}
result['add'] = adder(result)
return result

def adder(obj):
def add(value):
obj['data'] += value
return add

if __name__ == '__main__':
test = obj()
test['add']('world')
print test['data']
Nice :) Does anybody know how this would look in c code ?
Feb 8 '07 #3
#include <stdio.h>

function hello(){
struct obj = { char *data = 'hello'}
obj.add = obj_add(obj);
return obj;
}

function obj_add(obj){
function add(value){
obj.data += value;
return obj;
}
}

main(){
test = hello();
test.add('world');
printf(test.data);
}

I was thinking something like this maybe ?
Feb 8 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Steven C. | last post: by
4 posts views Thread by Akhil | last post: by
40 posts views Thread by gert | last post: by
reply views Thread by leo001 | last post: by

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.