How to make a function do reverse of its job Python [closed] - python

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 8 years ago.
Improve this question
How do you make a function do reverse of what it was programmed to do in Python without reprogramming the entire function?
For example:
swap1 = "abc"
swap2 = "def"
def swap():
global swap1, swap2
swap1 = swap2
into
swap1 = "abc"
swap2 = "def"
def swap():
global swap1, swap2
swap2 = swap1
Any help is appreciated.

A function can only do what it's designed to do. While some functions, by design, are reversible, there is no way to create a general case reverse function. Furthermore, many functions (notably hash functions) are intentionally designed to not be reversible, either simply because information is lost along the way, or because it is intentionally computationally impossible to reverse.
You would do better to rethink your design here - the pattern you demonstrated above using globals is highly undesirable, it will lead to very complex, difficult to maintain or understand code.

in general there is no such way to do this. the computer cannot know what you mean by "reverse", since there are different ways of the "reverse of a function".

No way to 'reverse' a function. Only rewriting it is possible. Sorry.

Related

How to make a calculator discord bot? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 months ago.
Improve this question
How to make a discord bot so when I input the following it calculates it:
INPUT:
2.2x100 2.8x100-10%
OUTPUT:
output should be the result of the following:
2.8100 - 2.2100 and then minus 10% of that,
(that equation is subject to change)
How would I do this?
This is not an easy task (if you don't want to be unsafe and eval() code).
You need an infix parser that takes into account order of operations (priorities). If there are parenthesis in your operation, it will be even more complex.
You will then need to evaluate the result with a function that executes an abstract syntax tree generated by the parser (AST).
There is probably libraries that make this easier without being unsafe, but make a google search about all the terms I talked about so you can learn more.
EDIT: also take a look at this link. Not beginner stuff.
Infix Calculator Expression Parser
EDIT 2: Just in case you want the easiest solution with eval(), be careful. A skilled attacker can destroy your computer with malicious code. If you just eval() whatever is sent to the bot, they could really destroy / take control of your computer. Be careful.

Variable Variable in django template [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 3 years ago.
Improve this question
view.py
variable = 'amount'
amount = 200
print(variable) #actual: 'amount' expected: 200
In such case, is it possible to use Variable Variable like PHP?
in php, I could do
$variable = 'amount';
$amount = 200;
echo $$variable;
I know it might be not a good practice, but sometimes it could provide very powerful features.
Thanks.
Neither Python nor Django's templates support variable variables... fortunately. It's a confusing language feature in PHP and we're better off without it - as you said, it's not even a good practice to use them.
Think of it as a bad habit in PHP that you'll be leaving behind now, even in PHP there are always better alternatives. Maybe someone thought it was a good idea at the time but modern best practices indicate otherwise.
If you feel the need for dynamic behaviour regarding variable names consider using a dictionary instead, see this post for details.

is the common practice in python to put __init__ in the beginning of class definition? [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 8 years ago.
Improve this question
I wonder whether the recommended practice is to put init in the beginning of class definition or to put interface functions first:
class examp1:
def ifmethod1(self):
pass
def ifmethod2(self):
pass
def __init__(self):
pass
VS.
class examp1:
def __init__(self):
pass
def ifmethod1(self):
pass
def ifmethod2(self):
pass
A lot of people put it first, but every one can have their own opinion and you'll find arguments for both.
To me, being first is simply useful because it's helpful to know __init__ quickly.
In an effort to help coders of other languages grasp Python quickly, ibiblio.org says "__init__ is analogous to a constructor". In that spirit, it would often be put first — constructors in other languages are almost always listed first.
In one example, the Google style guide for Python, it is always listed first.

python shorten nested unmutable object variable names [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 8 years ago.
Improve this question
This question is mostly just about curiosity.
As a java programmer myself, I often get frustrated by all the dots on nested variables. (There is kind of a de facto standard in java that any line with a variable with more than 2 dots needs to be re-written). Is there anyway to "rename" a nested variable such that I don't have to type all the dots everytime?
For example
class MyClass():
def my_func():
if not self.app.arguments.foo:
self.app.arguments.foo = 'bar'
print self.app.arguments.foo
Is there a way to write this such that I don't have to type the full name self.app.arguments.foo everytime?
Second, what is the standard 'pythonic' way of using variables as such. Would standard python nomenclature say just use all the dots all the time?
Extra notes
As I said, this is more about curiosity. So lets assume that I do not have access to module b. In other words I can not (more like don't want to) write getter and setter methods.
Also note that the self is important. I know I can do a rename with an import, but you cant import self.
You can use a temporary variable for all but the last item in a chain:
b = self.a.b
if b.c:
b.c = 'foo'
print b.c
If you weren't assigning, you could go all the way to c.
This is a little bit faster as well as being easier to write. However, it may or may not be easier to read.

Exit in a function using Python [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 8 years ago.
Improve this question
A quick function question in Python. Is it possible to stop a function from processing and return early. i.e like this:
def _do_something():
if not exists('/etc/', use_sudo=True):
# no files don't do any more
return
# if ok continue with stuff
sudo('/etc/init.d/nginx stop')
Is this ok? Or is there something like 'exit' ?
Your question sounds like " is there a better way to do this?".
no, that looks fine and will work.
For loops you could use the break-statement, but in a function this is good way to "stop" it - returning None implicitly.
Yes, this is a common way of structuring program logic.
Some people would argue that you write your function the other way around, like:
def _do_something():
if exists('/etc/', use_sudo=True):
sudo('/etc/init.d/nginx stop')
because it is shorter and, in a way, more directly represents what you mean by writing the function.
One thing to worry about with the structure that you have in your question is what happens when the function becomes more complicated. It is possible for a large and complicated function with many return statements to actually have unreachable code, where the function is guaranteed to return early and there is code at the end of the function that will not be executed.
def lol():
print "first!"
return
print "unreachable"
In this case, it is trivial to see that "unreachable" will never be printed, but if you have many nested if/else statements, this becomes less obvious. Good IDEs will warn you about unreachable code, but this is something to consider if you choose to program in a simpler text editor.
Side note: Just in case you weren't aware, Python functions that start with one or two underscores often have a "special meaning" attached to them. I'll leave the rest for you to explore.

Categories