Using MyClass(object) gone wrong? [duplicate] - python

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

Related

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]())

Extra "None" as output in python [duplicate]

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())

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.

Returning a function in Python [duplicate]

This question already has answers here:
Interesting 'takes exactly 1 argument (2 given)' Python error
(6 answers)
Closed 6 years ago.
I get the following error "TypeError: make_pretty() takes exactly 1 argument (2 given)" for the below code snippet. I'm learning python and any help would be helpful...
class test:
def make_pretty(func):
def inner():
print("I got decorated")
func()
return inner
def ordinary():
print("I am ordinary")
pretty1 = test()
pretty = pretty1.make_pretty(pretty1.ordinary)
print(pretty())
I also tried it with decorators, but still face the same error..
class SmartDivide:
def smart_divide(self,func):
def inner(a,b):
print("I am going to divide",a,"and",b)
if b == 0:
print("Whoops! cannot divide")
return
return func(a,b)
return inner
#smart_divide
def divide(self,a,b):
return a/b
dvd = SmartDivide()
print(dvd.divide(2,5))
dvd.divide(2,0)
You may need the self keyword
class test:
def make_pretty(self, func):
def inner():
print("I got decorated")
func()
return inner
def ordinary(self):
print("I am ordinary")
pretty1 = test()
pretty = pretty1.make_pretty(pretty1.ordinary)
print(pretty())

Syntax error: Invalid statement python-pygame [duplicate]

This question already has answers here:
Python print statement “Syntax Error: invalid syntax” [duplicate]
(2 answers)
Closed 9 years ago.
I am learning to make a role playing game in Python by watching some tutorials on Youtube. The guy didn't show me how to setup anything to get it working. I did get pygame and stuff working by watching other videos. Anyway here is my error and code:
#!C:\python32
class Character:
def __init__(self, name, hp):
self.name = name
self.hp = hp
c = Character("Test", 5)
print c.name
print c.hp
Error:
File "C:\Users\Johnathan\Desktop\My Game\character\character.py", line 8
print c.name
^
SyntaxError: invalid syntax
[Finished in 0.2s with exit code 1]
In python3 print is a function, not a statement.
Try:
print(c.name)
Also you are missing an indentation after class Character:. (Rule of thumb: After most colons follows either a single statement on the same line, or an indented suite of statements.) Your code should read:
class Character:
def __init__(self, name, hp):
self.name = name
self.hp = hp
c = Character("Test", 5)
print(c.name)
print(c.hp)

Categories

Resources