The Artima Developer Community
Sponsored Link

Python Answers Forum
Code Snippets

1 reply on 1 page. Most recent reply: Aug 22, 2003 8:26 AM by Merriodoc Brandybuck

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Nathan Olberding

Posts: 1
Nickname: nko
Registered: Aug, 2003

Code Snippets Posted: Aug 22, 2003 12:34 AM
Reply to this message Reply
Advertisement
Hi! I'm kinda new to programming. In Python, is there a way to insert a code snippet inline? Basically, I've got some recurring code, and rather than type it out every time, I'd like to be able to just write out one line of code that refers to some prewritten chunk. Kind of like a function, but not really. I could implement the same thing in a function, but my code would suddenly get more awkward. Am I doomed to go through what I've already done just to replace the ugly spots with slightly less ugly function calls?


Merriodoc Brandybuck

Posts: 225
Nickname: brandybuck
Registered: Mar, 2003

Re: Code Snippets Posted: Aug 22, 2003 8:26 AM
Reply to this message Reply
> Hi! I'm kinda new to programming. In Python, is there a
> way to insert a code snippet inline? Basically, I've got
> some recurring code, and rather than type it out every
> time, I'd like to be able to just write out one line of
> code that refers to some prewritten chunk. Kind of like a
> function, but not really. I could implement the same thing
> in a function, but my code would suddenly get more
> awkward. Am I doomed to go through what I've already done
> just to replace the ugly spots with slightly less ugly
> function calls?

I'm not sure what you're really after here. The closest thing I've ever seen, without really having a function call would be to assign a lambda function to a variable like this:

cs = lambda *args: ''.join(map(str,args))
print cs("a","b","c","d",1,2,3)

I got this from http://www.mindview.net/Books/Python/ThinkingInPython.html

Basically what you're asking about is similar to a goto in a lot of respects. Depending on how complex the logic is, you're probably best off just creating a function call. I'm not sure I understand how a function call would make your code that much more awkward than having a call to a code snippet. If you're using it that much where this is an issue, I would break it out now before you double the amount of times you call the code chunk.

If you have to update something on this six months from now, you'll thank yourself for having the logic in a single function call, rather than having to update multiple occurences of the same code.

Flat View: This topic has 1 reply on 1 page
Topic: MS Web Service client Previous Topic   Next Topic Topic: Just getting started

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use