I am stuck on Python Classes, exercise 3 on Everything is an object. I have created a basic function where I a just returning a string and my call is printing the results of the call wrapped inside the dir() function, but I am getting an error "Did I print dir() of this_function_is_an_object?
Here is my code:
return “this is a function”
myObj = this_function_is_an_object(“string”)
If I understand your question, you need to print the output of a function? first thing I notice is that your code is not well formatted that maybe causing the first error.
Also you assign the call of your function to a variable “myObj” that variable now has the value of the function return’s
which is “this is a function” for you to be able to see the output you will have to put the variable in a print statement and it will log out the value stored in the variable.
I don’t know what you trying to do on the last line if you want to call the function again and log out the output of the return you can remove the dir and just call the function in the print statement.
dir is used to get operations, methods and function that operates on a data type. One last thing I noticed is that your function takes in an argument/parameter but it was never used.
For a simple function that just print out something like this without manipulating any data the argument can be ignore and the return statement is unnecessary you can add the print statement in the function body.
I hope this help ):
Here is your code, but formatted:
return "this is a function"
#note, the way of separating two words in python is "_", unlike java.
my_obj = this_function_is_an_object("string")
Again. like @array1752550387 said, I see no clear reason for the last line. If you are trying to use
print() to print something out, then the
dir() is unnecessary. It would help to clarify what you are doing. Also, I have not gotten far enough to learn the
dir() function, so I think I can help you more after I’ve fully learned it.
We can use the
dir() function to investigate an object’s attributes at runtime.
dir() is short for directory and offers an organized presentation of object attributes. This exercise on classes is just a way of using the dir() function to show what built in classes/modules, etc are available. The exercise wanted you to create a function to accept ANY input and return anything you want and then using the dir() function to return what is available when you call a function
This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.