Better way to implement interpreter in Python [closed] - python

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I am attempting to implement an interpreter for brainfuck and as of now, I am just using a series of if/elif statements.
if(i == ">"):
...
elif(i == "<"):
...
elif(i == "+"):
...
elif(i == "-"):
...
However, this seems very clunky and un-pythonic to me. Is there a better (cleaner/faster/more aesthetically pleasing) way to implement this?

I have a quick implementation of a Brainfuck interpreter for Python in a GitHub repo. In a nutshell, though, you could keep a dictionary, where the keys are the Brainfuck characters and the values are function (or method) objects, and then dispatch on that. Something like this:
instructions = {
'+': increment,
'-': decrement,
# Other functions
}
def run(tape):
ch = next_token(tape)
if ch in instructions:
instructions[ch]()
(Not an actual implementation, just a quick illustration.)

Related

Variable naming conventions for function variables [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
beginner here. I'm writing a script that has a few related functions and am wondering what the standards are for naming the variables within my functions. For example if I have function_1 and function_2 that both a take some sort of file, is it acceptable to name both variables file? I know it will work, but is that horrible coding practice or is it alright to do?
def function_1(file):
# Do something
return file
def function_2(file):
# Do something
return file
def main():
file_1 = function_1(file)
file_2 = function_2(file)
if __name__ == "__main__":
main()
Since the two are parameters within the scope of two different functions, it is completely fine to name them the same. However, since file is a builtin in Python, I would suggest to name it differently or to add an underscore at the end of the name.

Return a value or assigning a value to a variable and then return - best practice? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Suppose I have two functions
def sum(a, b):
result = a + b
return result
and
def sum(a, b):
return a + b
From the point of view of good programming practices and software engineering, which solution is better? Return a value or assigning a value to a variable and then return? Why?
2nd option is good because it saves space of a variable but at times, we use more variables on purpose for the sake of clarity. We always have to maintain a good balance between clarity and space.

Function doesn't Break [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am trying to break out or restart a function in python 2. Putting a return statement should stop all execution. Same goes for restarting the function.
def function():
...
if len(lst) == 1:
return value
print 'foo'
else:
function()
print 'foo'
In this case 'foo' would be printed twice.
The return statement only stops the execution of the current instance of the function.
Since you're doing a recursion, you will still get the other calls of that function running until they hit their own return (if any).

Why is it bad to append dictionaries with x = dict(x, **y)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
The refutations in this thread make no sense to me whatsoever. Can someone break it down? Make analogies? Why is this an "abusive hack"?
dict(x, **foo)
...given foo = { 'hello': 'world' }, does the following:
dict(x, hello=world)
This is reasonably straightforward -- however, kwargs behavior is only well-defined for keys which actually could be passed as keyword arguments in (all available versions of) Python. Consider something like:
foo = { ('some', 'tuple'): 'value' }
...in which case you have a key which couldn't actually be passed as a keyword argument; to pass **foo would be to exercise behavior which is not intuitively defined to readers, and which some versions of Python (such as Python 3) will explicitly reject.
By contrast:
x = dict(x) # create a new object
x.update(y)
...is relying only on well-defined behavior, and is portable going forward.

Which of these two functions more "pythonic"? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I just want to know which way is more preferable in python.
Imagine two functions:
1 function:
def foo(key):
if bar.has_key(key):
return bar.get(key)
# do something with bar
# this will be executed if bar_key(key) is False
...
return something
2 function:
def foo(key):
if bar.has_key(key):
return bar.get(key)
else:
# do something with bar
# this will be executed if bar_key(key) is False
...
return something
As you can see the only difference is else statement. So the question is will it affect performance somehow. Or are there any reasons to include else in this type of functions?
If the choice is between those two approaches, I would pick the first one. return is pretty explicit that execution terminates at that point. I find if x { return y } else { ... } an anti-pattern for this reason (not just in Python -- I see this in C/C++ code and it irks me there, too).
If you are returning, an else block is entirely unnecessary and causes pointless indentation of a block of code that might be quite large. The more nested structures you have, the more difficult it is to maintain proper context in your head while reading code. For this reason I tend to prefer less nesting when it does not obfuscate logic, and in this case I don't think it would.
The pythonic way:
def foo(key):
return bar.get(key, something)
While this question is a little opinion based, I'd say the second is more Pythonic for the reason of "explicit is better than implicit". The second function is clearly saying "if this condition, do this. Otherwise, do this". On the other hand, the first function implies the "Otherwise" part.

Categories

Resources