Proper way to state a condition trough functions
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I've got a code with different functions. Inside one of them there is a condition. I have to check if this condition occur to execute another function.
What's the proper way to do that? I've tried something like this but it doesn't work
Example:
class MyClass:
def thisFunction(self):
try:
"I'm doing things"
except:
self.stop = print("That's already done!")
def thisOtherFunction(self):
"I'm doing things with things done in thisFunction"
s = MyClass()
s.thisFunction()
if self.stop == None:
s.thisOtherFunction()
else:
pass
Thanks a lot!
Update
Actually it's a lot simplier doing:
class MyClass:
def thisFunction(self):
try:
"I'm doing things"
except:
self.stop = print("That's already done!")
def thisOtherFunction(self):
try:
"I'm doing things with things done in thisFunction"
except:
pass
s = myClass()
s.thisFunction()
s.thisOtherFunction()
Thanks to Adam Smiths's example, I simply didn't think about that. Maybe it's not so much elegant, though.
Update2
Another way is to use def __init__
in this way:
class MyClass:
def __init__(self):
self.commandStop = False
def thisFunction(self):
try:
"I'm doing things"
except:
self.commandStop = True
def thisOtherFunction(self):
"I'm doing things with things done in thisFunction"
def conditionToGo(self):
if self.commandStop == False:
print("That's already done!")
else:
s.thisOtherFunction()
s = myClass()
s.thisFunction()
s.conditionToGo()
python python-3.x function
add a comment |
I've got a code with different functions. Inside one of them there is a condition. I have to check if this condition occur to execute another function.
What's the proper way to do that? I've tried something like this but it doesn't work
Example:
class MyClass:
def thisFunction(self):
try:
"I'm doing things"
except:
self.stop = print("That's already done!")
def thisOtherFunction(self):
"I'm doing things with things done in thisFunction"
s = MyClass()
s.thisFunction()
if self.stop == None:
s.thisOtherFunction()
else:
pass
Thanks a lot!
Update
Actually it's a lot simplier doing:
class MyClass:
def thisFunction(self):
try:
"I'm doing things"
except:
self.stop = print("That's already done!")
def thisOtherFunction(self):
try:
"I'm doing things with things done in thisFunction"
except:
pass
s = myClass()
s.thisFunction()
s.thisOtherFunction()
Thanks to Adam Smiths's example, I simply didn't think about that. Maybe it's not so much elegant, though.
Update2
Another way is to use def __init__
in this way:
class MyClass:
def __init__(self):
self.commandStop = False
def thisFunction(self):
try:
"I'm doing things"
except:
self.commandStop = True
def thisOtherFunction(self):
"I'm doing things with things done in thisFunction"
def conditionToGo(self):
if self.commandStop == False:
print("That's already done!")
else:
s.thisOtherFunction()
s = myClass()
s.thisFunction()
s.conditionToGo()
python python-3.x function
add a comment |
I've got a code with different functions. Inside one of them there is a condition. I have to check if this condition occur to execute another function.
What's the proper way to do that? I've tried something like this but it doesn't work
Example:
class MyClass:
def thisFunction(self):
try:
"I'm doing things"
except:
self.stop = print("That's already done!")
def thisOtherFunction(self):
"I'm doing things with things done in thisFunction"
s = MyClass()
s.thisFunction()
if self.stop == None:
s.thisOtherFunction()
else:
pass
Thanks a lot!
Update
Actually it's a lot simplier doing:
class MyClass:
def thisFunction(self):
try:
"I'm doing things"
except:
self.stop = print("That's already done!")
def thisOtherFunction(self):
try:
"I'm doing things with things done in thisFunction"
except:
pass
s = myClass()
s.thisFunction()
s.thisOtherFunction()
Thanks to Adam Smiths's example, I simply didn't think about that. Maybe it's not so much elegant, though.
Update2
Another way is to use def __init__
in this way:
class MyClass:
def __init__(self):
self.commandStop = False
def thisFunction(self):
try:
"I'm doing things"
except:
self.commandStop = True
def thisOtherFunction(self):
"I'm doing things with things done in thisFunction"
def conditionToGo(self):
if self.commandStop == False:
print("That's already done!")
else:
s.thisOtherFunction()
s = myClass()
s.thisFunction()
s.conditionToGo()
python python-3.x function
I've got a code with different functions. Inside one of them there is a condition. I have to check if this condition occur to execute another function.
What's the proper way to do that? I've tried something like this but it doesn't work
Example:
class MyClass:
def thisFunction(self):
try:
"I'm doing things"
except:
self.stop = print("That's already done!")
def thisOtherFunction(self):
"I'm doing things with things done in thisFunction"
s = MyClass()
s.thisFunction()
if self.stop == None:
s.thisOtherFunction()
else:
pass
Thanks a lot!
Update
Actually it's a lot simplier doing:
class MyClass:
def thisFunction(self):
try:
"I'm doing things"
except:
self.stop = print("That's already done!")
def thisOtherFunction(self):
try:
"I'm doing things with things done in thisFunction"
except:
pass
s = myClass()
s.thisFunction()
s.thisOtherFunction()
Thanks to Adam Smiths's example, I simply didn't think about that. Maybe it's not so much elegant, though.
Update2
Another way is to use def __init__
in this way:
class MyClass:
def __init__(self):
self.commandStop = False
def thisFunction(self):
try:
"I'm doing things"
except:
self.commandStop = True
def thisOtherFunction(self):
"I'm doing things with things done in thisFunction"
def conditionToGo(self):
if self.commandStop == False:
print("That's already done!")
else:
s.thisOtherFunction()
s = myClass()
s.thisFunction()
s.conditionToGo()
python python-3.x function
python python-3.x function
edited Nov 16 '18 at 17:31
Lara M.
asked Nov 16 '18 at 16:36
Lara M.Lara M.
428418
428418
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I've made patterns before where I had to do a series of transforms to a value and it needs to pass a test each time. You could construct that with:
def pipeline(predicate, transformers):
def wrapped(value):
for transformer in transformers:
value = transformer(value)
if not predicate(value):
raise ValueError(f"{value} no longer satisfies the specified predicate.")
return value
return wrapped
Then, to construct an example, let's say I need to do some math on a number but ensure that the number never goes negative.
operations = [
lambda x: x+3,
lambda x: x-10,
lambda x: x+1000,
lambda x: x//2
]
job = pipeline(lambda x: x>0, operations)
job(3) # fails because the sequence goes 3 -> 6 -> (-4) -> ...
job(8) # is 500 because 8 -> 11 -> 1 -> 1001 -> 500
You know, I've taken your example and made a simple edit to my code, inserting a try there def thisOtherFunction(self): try: "I'm doing things with things done in thisFunction" except: pass s = MyClass() s.thisFunction() s.thisOtherFunction() So, a lot more simple than that I thought.
– Lara M.
Nov 16 '18 at 17:03
It's not exactly your example, but I'm not so skilled for that!
– Lara M.
Nov 16 '18 at 17:11
1
@LaraM. note that unless you're handling that logic in the parts of your code you haven't posted, nothing is going to stopthisOtherFunction
from running ifthisFunction
throws its error.
– Adam Smith
Nov 16 '18 at 17:15
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53341988%2fproper-way-to-state-a-condition-trough-functions%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I've made patterns before where I had to do a series of transforms to a value and it needs to pass a test each time. You could construct that with:
def pipeline(predicate, transformers):
def wrapped(value):
for transformer in transformers:
value = transformer(value)
if not predicate(value):
raise ValueError(f"{value} no longer satisfies the specified predicate.")
return value
return wrapped
Then, to construct an example, let's say I need to do some math on a number but ensure that the number never goes negative.
operations = [
lambda x: x+3,
lambda x: x-10,
lambda x: x+1000,
lambda x: x//2
]
job = pipeline(lambda x: x>0, operations)
job(3) # fails because the sequence goes 3 -> 6 -> (-4) -> ...
job(8) # is 500 because 8 -> 11 -> 1 -> 1001 -> 500
You know, I've taken your example and made a simple edit to my code, inserting a try there def thisOtherFunction(self): try: "I'm doing things with things done in thisFunction" except: pass s = MyClass() s.thisFunction() s.thisOtherFunction() So, a lot more simple than that I thought.
– Lara M.
Nov 16 '18 at 17:03
It's not exactly your example, but I'm not so skilled for that!
– Lara M.
Nov 16 '18 at 17:11
1
@LaraM. note that unless you're handling that logic in the parts of your code you haven't posted, nothing is going to stopthisOtherFunction
from running ifthisFunction
throws its error.
– Adam Smith
Nov 16 '18 at 17:15
add a comment |
I've made patterns before where I had to do a series of transforms to a value and it needs to pass a test each time. You could construct that with:
def pipeline(predicate, transformers):
def wrapped(value):
for transformer in transformers:
value = transformer(value)
if not predicate(value):
raise ValueError(f"{value} no longer satisfies the specified predicate.")
return value
return wrapped
Then, to construct an example, let's say I need to do some math on a number but ensure that the number never goes negative.
operations = [
lambda x: x+3,
lambda x: x-10,
lambda x: x+1000,
lambda x: x//2
]
job = pipeline(lambda x: x>0, operations)
job(3) # fails because the sequence goes 3 -> 6 -> (-4) -> ...
job(8) # is 500 because 8 -> 11 -> 1 -> 1001 -> 500
You know, I've taken your example and made a simple edit to my code, inserting a try there def thisOtherFunction(self): try: "I'm doing things with things done in thisFunction" except: pass s = MyClass() s.thisFunction() s.thisOtherFunction() So, a lot more simple than that I thought.
– Lara M.
Nov 16 '18 at 17:03
It's not exactly your example, but I'm not so skilled for that!
– Lara M.
Nov 16 '18 at 17:11
1
@LaraM. note that unless you're handling that logic in the parts of your code you haven't posted, nothing is going to stopthisOtherFunction
from running ifthisFunction
throws its error.
– Adam Smith
Nov 16 '18 at 17:15
add a comment |
I've made patterns before where I had to do a series of transforms to a value and it needs to pass a test each time. You could construct that with:
def pipeline(predicate, transformers):
def wrapped(value):
for transformer in transformers:
value = transformer(value)
if not predicate(value):
raise ValueError(f"{value} no longer satisfies the specified predicate.")
return value
return wrapped
Then, to construct an example, let's say I need to do some math on a number but ensure that the number never goes negative.
operations = [
lambda x: x+3,
lambda x: x-10,
lambda x: x+1000,
lambda x: x//2
]
job = pipeline(lambda x: x>0, operations)
job(3) # fails because the sequence goes 3 -> 6 -> (-4) -> ...
job(8) # is 500 because 8 -> 11 -> 1 -> 1001 -> 500
I've made patterns before where I had to do a series of transforms to a value and it needs to pass a test each time. You could construct that with:
def pipeline(predicate, transformers):
def wrapped(value):
for transformer in transformers:
value = transformer(value)
if not predicate(value):
raise ValueError(f"{value} no longer satisfies the specified predicate.")
return value
return wrapped
Then, to construct an example, let's say I need to do some math on a number but ensure that the number never goes negative.
operations = [
lambda x: x+3,
lambda x: x-10,
lambda x: x+1000,
lambda x: x//2
]
job = pipeline(lambda x: x>0, operations)
job(3) # fails because the sequence goes 3 -> 6 -> (-4) -> ...
job(8) # is 500 because 8 -> 11 -> 1 -> 1001 -> 500
answered Nov 16 '18 at 16:54
Adam SmithAdam Smith
35.5k73377
35.5k73377
You know, I've taken your example and made a simple edit to my code, inserting a try there def thisOtherFunction(self): try: "I'm doing things with things done in thisFunction" except: pass s = MyClass() s.thisFunction() s.thisOtherFunction() So, a lot more simple than that I thought.
– Lara M.
Nov 16 '18 at 17:03
It's not exactly your example, but I'm not so skilled for that!
– Lara M.
Nov 16 '18 at 17:11
1
@LaraM. note that unless you're handling that logic in the parts of your code you haven't posted, nothing is going to stopthisOtherFunction
from running ifthisFunction
throws its error.
– Adam Smith
Nov 16 '18 at 17:15
add a comment |
You know, I've taken your example and made a simple edit to my code, inserting a try there def thisOtherFunction(self): try: "I'm doing things with things done in thisFunction" except: pass s = MyClass() s.thisFunction() s.thisOtherFunction() So, a lot more simple than that I thought.
– Lara M.
Nov 16 '18 at 17:03
It's not exactly your example, but I'm not so skilled for that!
– Lara M.
Nov 16 '18 at 17:11
1
@LaraM. note that unless you're handling that logic in the parts of your code you haven't posted, nothing is going to stopthisOtherFunction
from running ifthisFunction
throws its error.
– Adam Smith
Nov 16 '18 at 17:15
You know, I've taken your example and made a simple edit to my code, inserting a try there def thisOtherFunction(self): try: "I'm doing things with things done in thisFunction" except: pass s = MyClass() s.thisFunction() s.thisOtherFunction() So, a lot more simple than that I thought.
– Lara M.
Nov 16 '18 at 17:03
You know, I've taken your example and made a simple edit to my code, inserting a try there def thisOtherFunction(self): try: "I'm doing things with things done in thisFunction" except: pass s = MyClass() s.thisFunction() s.thisOtherFunction() So, a lot more simple than that I thought.
– Lara M.
Nov 16 '18 at 17:03
It's not exactly your example, but I'm not so skilled for that!
– Lara M.
Nov 16 '18 at 17:11
It's not exactly your example, but I'm not so skilled for that!
– Lara M.
Nov 16 '18 at 17:11
1
1
@LaraM. note that unless you're handling that logic in the parts of your code you haven't posted, nothing is going to stop
thisOtherFunction
from running if thisFunction
throws its error.– Adam Smith
Nov 16 '18 at 17:15
@LaraM. note that unless you're handling that logic in the parts of your code you haven't posted, nothing is going to stop
thisOtherFunction
from running if thisFunction
throws its error.– Adam Smith
Nov 16 '18 at 17:15
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53341988%2fproper-way-to-state-a-condition-trough-functions%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown