Easy questions from a python beginner

Stephen Hansen me+list/python at ixokai.io
Fri Jul 23 00:23:05 EDT 2010


On 7/22/10 7:47 PM, wheres pythonmonks wrote:
> Thanks for pointing out that swap (and my swap2) don't work everywhere
> -- is there a way to get it to work inside functions?
> 
> "No offense, but you seem like you're still tying to be a hacker.  If
> that's what you want, fine, but generally speaking (and particularly
> for Python), you are going to have a better experience if you do it
> the language's way."
> 
> None taken, but I always think that it is the language's job to
> express my thoughts...  I don't like to think that my thoughts are
> somehow constrained by the language.

The thing is, every language is adept at expressing different kinds of
abstractions. They have different goals and ways of encouraging certain
types of solutions while discouraging (and many times simply not even
trying to offer support for) others: you're going up against those
'discouraged' very, very hard-- and you're going to hit a wall :) The
Python language does constrain your expressiveness in these sorts of
areas, and there's no way around it.

If that doesn't suit you-- you're free to use another language which
works better to how your brain works. Ruby actually might be a very good
suggestion. I don't mean this as a 'Deal with it or f- off, loser'
statement. :) I mean that Python uses a very simple model in how it
handles namespaces, calling and similar -- a lot of what seems
completely natural to express in other languages is either slow,
convoluted, complicated or flatly impossible here. On purpose.

But, for those things that aren't expressed easily in Python, there's
almost always a *different way* to express the actual need and
intention-- in a way that is elegant and direct, in Python. You can't do
it the other way "Pythonic", because "Pythonic" is to *not* do it that
way. To not do things like try to treat variables as if they had
box-like-behavior.

> The truth is that I don't intend to use these approaches in anything
> serious.  However, I've been known to do some metaprogramming from
> time to time.

Depending on how you define "metaprogramming", Python is pretty
notoriously ill-suited towards the task (more, its basically considered
a feature it doesn't let you go there) -- or, it allows you to do vast
amounts of stuff with its few dark magic hooks. I've never had a
satisfying definition of metaprogramming that more then 50% of any group
agree with, so I'm not sure which you're looking for. :)

Python allows you to easily override nearly all operations and
interactions between objects, construct classes, instances and such at
runtime and modify them at any time. If metaprogramming is this to you,
basically runtime tweaking and modification of classes types objects and
the like, Python's good to go.

But! What it doesn't let you do is get clever with syntax and write a
sort of "simplified" or domain specific language to achieve certain
sorts of repetitive tasks quickly. You always end up writing normal
Python that looks like all other Python. You just might have a few more
__methods__ and learn some dark magic like metaclasses and descriptors
and decorators. (Not that -all- uses of such are dark magic, but when
you start combining those your code can start to resemble a magic spell
doing unholy things-- like my library with classes that have
argument-based method overloading, so x.test("hi") and x.test("hi", 2)
call two totally different functions. Not that I'll ever admit to having
written the beast, nor that I actually did more then use it for pure
proof of concept, and neither shall I willingly ever show anyone the
monstrosity).

> In a recent application, I pass in a list of callables (lambdas) to be
> evaluated repeatedly.
> Clearly, a superior solution is to pass a single lambda that returns a
> list. [Less function call dispatches]

> However, it might be more efficient to avoid the function call
> overhead completely and pass-in a string which is substituted into a
> string code block, compiled, and executed.

This is the kind of "metaprogramming" where Python says nyet, that first
kind I mentioned about being Notoriously Ill Suited. You can do things
like compile code dynamically, generate up bytecode even, but it'll
never be more efficient.

Use a generator instead. :)

-- 

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 487 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20100722/7e9b62ec/attachment-0001.sig>


More information about the Python-list mailing list