|
Dynamic code - Python
When I learned to program, named blocks of code (subroutines and functions) were built into each program and weren't things you could change at run time. But these days, in some languages like Python it's very different and you can write code that includes dynamic functions and methods - after all, in Python everything is an objects (including code) and you can change objects as your program runs.
What does this mean?
Well - here's a piece of valid code:
if kids < 3:
def more(current):
return current + 3
else:
def more(current):
return current + 8
and in that code I've defined a function in two different ways depending on the value of a variable. I can also redefine it later on in my code if I wish - you can see that in a full example here.
Is this capability a good idea? First comment - be careful how you use it, as it's open to abuse. BUT, yes, it can be very effective. For example, a complete application such as Plone can have default functions defined and they a user-supplied piece of code can replace a named function - a really useful, practical, and powerful facility. (written 2008-11-11 23:11:41)
Associated topics are indexed under Y105 - Python - Functions, Modules and Packages
Some other Articles
Spa Road Garage and Convenience Store, MelkshamWiltshire Council says - part 2Wiltshire Council says - part 1Melksham Chamber of Commerce, or the FSBDynamic code - PythonPascals Triangle in Python and JavaGuests doing a bunk?Python Regular ExpressionsWhat are exceptions - Python based answerIs it worth it?
|
1975 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 at 50 posts per page
This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).
|
|