Reputation: 123
I am new in python and I was trying to understand how does self work. As I came from JavaScript I think its bit similar to this. If I am wrong please correct me. In the meantime I wrote this code:
class Restaurant(object):
bankrupt = False
def open_branch(self):
if not self.bankrupt:
print("branch opened")
x = Restaurant()
y = Restaurant()
y.bankrupt = True
print(y.open_branch())
print(x.open_branch())
and got the output:
None
branch opened
None
As I called the open_branch() twice. It should not print thrice. I think it should print,
branch opened // for first call
None // for 2nd call
you could paste the code here and see Please somebody explain this.
Upvotes: 0
Views: 530
Reputation: 14689
The two print statements at the end are only printing the returned results of the method:
print(y.open_branch())
print(x.open_branch())
Since python returns None
by default if you don't explicitly specify a return statement, and you are calling the methods twice, then it's expected that you see two None
statements.
If you call the method without print, you will see a single printed message.
y.open_branch()
x.open_branch()
You could improve your code by adding return statements within the method and doing the print outside, as you are doing right now. Something like
class Restaurant(object):
bankrupt = False
def open_branch(self):
if not self.bankrupt:
return "branch opened"
return "branch closed"
x = Restaurant()
y = Restaurant()
y.bankrupt = True
print(y.open_branch())
# output:'branch closed'
print(x.open_branch())
# output: 'branch opened'
Upvotes: 2
Reputation:
There are a few things that are giving you issues here:
Your open_branch
function isn't returning anything, so when you call print(self.open_branch(x))
it's printing None
, which is what all functions in Python return by default if there is no return
statement. You can solve this issue by replacing
if not self.bankrupt:
print("branch opened")
with
if not self.bankrupt:
return "branch opened"
You're calling print
three times, which is why you're getting three lines printed on the console (although this will resolve itself if you replace the print
in the function with the return
statement I detailed above).
You should put an else
statement under your if
clause, so that it will return something useful (instead of None
if self.bankrupt == True
) and you can tell if the branch was opened or not. I'd suggest something like:
if not self.bankrupt:
return "branch opened"
else:
return "Restaurant is bankrupt, branch could not be opened"
Upvotes: 1
Reputation: 2224
Ok, you have the following code:
class Restaurant(object):
bankrupt = False
def open_branch(self):
if not self.bankrupt:
print("branch opened")
x = Restaurant()
y = Restaurant()
till here you have two objects of type 'Restaurant'.
y.bankrupt = True
Here you changed the attribute bankrupt
of the object stored in the variable y
print(y.open_branch())
This will output
None
only; why? Well first of all you are calling the function y.open_branch()
but this function will not output anything because the attribute bankrupt
is true so if not self.bankrupt
is evaluated to false and thus the print statement is not executed. However the function open_branch()
is also not returning anything (there is no return true
statement or so. So print(y.open_branch())
is equal to print(None)
which will output None
. Next
print(x.open_branch())
For x the attribut bankrupt
is false, so if not self.bankrupt
is true and the print statement print("branch opened")
will get evaluated. Afterwards None
will be outputted for the same reason as why None
got outputted by print(y.open_branch())
.
Upvotes: 2
Reputation: 955
What you're doing is you're calling print
on the return value of open_branch
.
So when you see it print None
, you're seeing the outcome of print(x.open_branch()
and print(y.open_branch()
.
When you see branch opened
, that's the result of the print inside open_branch
.
Try this:
class Restaurant(object):
bankrupt = False
def open_branch(self):
result = ""
if self.bankrupt:
result = "Restaurant is bankrupt"
else:
result = "Branch opened"
return result
x = Restaurant()
y = Restaurant()
y.bankrupt = True
print(y.open_branch())
print(x.open_branch())
Upvotes: 1