Extra "None" as output in python [duplicate] - python

This question already has answers here:
Why is "None" printed after my function's output?
(7 answers)
Closed 4 years ago.
class student:
birth_day = 21
birth_month = 4
birth_year = 1998
def __init__(self,name):
self.naav = name
def SayHi(self):
return print('hello'+''+self.naav)
Topper = student('vikas')
print(Topper.naav)
print(Topper.SayHi())
print(student.birth_day)
print(Topper.birth_day)
#print(student.naav)
The output to this is
vikas
hellovikas
None
21
21
I am confused with third output "None",am not sure how it works ,somebody help me understand

This is happening because on print(Topper.SayHi()) you are printing what the function SayHi returns, which is nothing (None).
This is because print('hello'+''+self.naav) doesn't return a value, it prints and returns nothing.
What you should do is return only the string, then print the return of the function SayHi (as you're already doing).
class student:
...
def SayHi(self):
return 'hello' + self.naav
...
print(Topper.SayHi())

Related

TypeError: module() takes at most 2 arguments (3 given) code taken from pluralsight course [duplicate]

This question already has answers here:
TypeError: module.__init__() takes at most 2 arguments (3 given)
(5 answers)
Closed 3 years ago.
I am viewing the Pluralsight course on Python. At the end of the module we are to write a Python script. The author does not show how to create two of the scripts. I have them coded as the follows:
main.py
from hs_student import *
james = HighSchoolStudent("james")
print(james.get_name_capitalize)
student.py
students = []
class Student:
school_name = "Springfield Elementary"
def __init__(self, name, s_id=332):
self.name = name
self.s_id = s_id
students.append(self)
def get_name_capitalize(self):
return self.name.capitalize()
...
hs_student.py
import student as student
students = []
class HighSchoolStudent(student):
school_name = "Springfield High School"
def get_school_name(self):
return "This is a High School student"
def get_name_capitalize(self):
original_value = super().get_name_capitalize()
return original_value + "-HS"
...
When running the code, I get an error. From my understanding, I am passing too many arguments to the get_name_capitalize function. How can I fix this?
The error message is:
TypeError: module() takes at most 2 arguments (3 given)
This code:
class HighSchoolStudent(student):
is trying to inherit from the student module, not the student.Student class. Change it to:
class HighSchoolStudent(student.Student):
to inherit from the intended class.

Python. How to call a function from a dictionary? [duplicate]

This question already has an answer here:
What does <function at ...> mean [duplicate]
(1 answer)
Closed 4 years ago.
I want to have a dictionary where a certain key calls a value... that value is a function and I would want that function to execute.
Below is my attempt to do it but all I get is the value of where it is stored in memory.
class Testing:
def __init__(self):
self.method = {'Method1': self.another, 'Method2': self.there}
def launch(self, input):
print(self.method[input])
#staticmethod
def another():
print('This print statement should pop out.')
#staticmethod
def there():
print('This should not appear.')
new = Testing()
new.launch('Method1')
The result that I get from that is:
<function Testing.another at 0x01519540>
Is there a way to do this?
You are missing the actual function call: (notice the added () at the end)
def launch(self, input):
print(self.method[input]())

Using MyClass(object) gone wrong? [duplicate]

This question already has answers here:
I'm getting an IndentationError. How do I fix it?
(6 answers)
Closed 4 years ago.
class Myclass(object):
def __init__(self , msg , integer):
self.msg = msg
self.integer = integer
print (self.msg)
print (self.integer)
return
Error
File "<input>", line 3
self.msg = msg
^
IndentationError: expected an indented block
This question is a duplicate, and fyi Python2 allows mixed usage of tabs and spaces but Python3 does not. It recommends using spaces for uniformity.
Please check PEP8 for more info:
https://www.python.org/dev/peps/pep-0008/#tabs-or-spaces
EDIT:
And please indent after defining a function. It must be all indented up to the code block.
class Myclass(object):
def __init__(self , msg , integer):
self.msg = msg
self.integer = integer
print (self.msg)
print (self.integer)
return
Whitespace matters in python.
Edit: Ran without issues
>>> class MyClass(object):
... def __init__(self, msg, integer):
... self.msg=msg
... self.integer = integer
... print(self.msg)
...
>>> x = MyClass('foo', 5)
foo

Calling out a function, not defined error [duplicate]

This question already has answers here:
How can I call a function within a class?
(2 answers)
Closed 3 years ago.
I'm new to making classes and I'm trying to complete exercise 9-1 in my 'Python Crash Course' book where the last part of the question asks me to call back my method but I end up getting
'not defined error' for describe_restaurant().
Here is my code:
class Restaurant():
def __init__(self, r_name, c_type):
self.r_name = r_name
self.c_type = c_type
def describe_restaurant():
print(self.r_name.title())
print(self.c_type.title())
def open_restaurant():
print(self.r_name + " is now open!")
Restaurant = Restaurant('Joe\'s Sushi', 'sushi')
print(Restaurant.r_name)
print(Restaurant.c_type)
describe_restaurant()
open_restaurant()
I thought that describe_restaurant shouldn't need to be defined though because I'm calling it out as a function to use?
Try:
class Restaurant():
def __init__(self, r_name, c_type):
self.r_name = r_name
self.c_type = c_type
def describe_restaurant(self):
print(self.r_name)
print(self.c_type)
def open_restaurant(self):
return "{} is now open!".format(self.r_name)
restaurant = Restaurant('Joe\'s Sushi', 'sushi')
print(restaurant.r_name)
print(restaurant.c_type)
restaurant.describe_restaurant()
restaurant.open_restaurant()
You need to create a class instance and call it's functions. In addition, as mentioned in the comments, you need to pass self to the instance methods. A short explanation of this can be found here.

python: how list increase in a object [duplicate]

This question already has answers here:
How to avoid having class data shared among instances?
(7 answers)
Should I use instance or class attributes if there will only be one instance? [closed]
(4 answers)
Closed 7 years ago.
Could you help me to find the problems?
I just add the object to one list, but it seems that it appears in another list!
class people(object):
name = ""
__adjacent = []
position = 0
def __init__(self,nam):
self.name = nam
def print_name(self):
print self.name
def get_adjacent(self):
return self.__adjacent
def append_adjacent(self,people):
self.__adjacent.append(people)
sis = people('sister')
bro = people('brother')
sis.append_adjacent(bro)
print len(bro.get_adjacent())
for i in bro.get_adjacent():
i.print_name()
print len(sis.get_adjacent())
Question: why there is a object in bro's adjacent list???

Categories

Resources