Setting function attribute from outside class

Edward C. Jones edcjones at erols.com
Sat May 24 14:51:13 EDT 2003


The following code seems to work (Python 2.2.2). Is this the best way of 
setting a function attribute from outside a class? How would this be 
done in new classes (class Node(object) ...)?

#! /usr/bin/env python

def fun(node):
     print node.i, 'in "fun"'

class Node:
     def __init__(self):
         self.i = 77

class Tree:
     def __init__(self, attribute_name):
         Node.__dict__[attribute_name] = fun

tree = Tree('abc')
node = Node()
node.abc()





More information about the Python-list mailing list