manel_star
manel_star

Reputation: 1

Calling a function in Pycharm

I am new in Python and I am currently learning OOP in Pycharm. When I type in a simple function like type(mylist), I dont see the answer in the console, I have to add print in the beginning, same with any other function, although in the tutorials I am currently following, they just call the function by typing its name and adding a parameter. Same with my first attribute (please see screenshots) Please help me if you know how to get around it.

enter image description here

Upvotes: 0

Views: 1131

Answers (3)

HolderRoberts
HolderRoberts

Reputation: 136

In python variable_name = expression can't be regarded as expression to be used as parameter, so print(my_dog=Dog(mybreed='lab')) will raise an error.

You can sure finish your job by this way:

my_dog = Dog(mybreed='lab')   # assign the variable my_dog
print(my_dog)                 # print the variable my_dog

If you don't need variable my_dog, you can just use print(Dog(mybreed='lab')), which will surely work.

If you do prefer assign a variable and pass it as a parameter (just like C++ does), you can use Assignment Expressions(also The Walrus Operator) := in Python 3.8 or higher version: print(my_dog:=Dog(mybreed='lab'))

But just keep it in mind that this operator maybe not as convenient as you think!

Upvotes: 0

Harry Sackman
Harry Sackman

Reputation: 13

Instead of: print(my_dog=Dog(mybreed='lab')) You could either split it to two lines:

my_dog = Dog(mybreed='lab')
print(my_dog)

Or, if you don't need the my_dog variable:

print(Dog(mybreed='lab'))

Upvotes: 0

Guy
Guy

Reputation: 50819

You need to separate the object instantiation from the print()

my_dog = Dog(mybreed='lab')
print(my_dog)

Upvotes: 2

Related Questions