Python - passing class instantiation to a function


Python - passing class instantiation to a function



I need to pass the class instantiation to a function.
This is what i wrote so far:


def func(dog):
print(dog.get_name)

def main():
new_dog = dog(name, age, weight)
func(new_dog)



but when i try to execute it i get the following error message:


<bound method animal.get_name of <classes.dog object at 0x7f9611002b70>



Basically i have a class file with the following classes:



What i'm doing wrong?



--EDIT--
Structure of the classes:


class animal:
# public vars
name = ""
age = 0
weight = 0.00
animal_type = ""

# public constructor
def __chars(self):
print("His name is: " + self.name)
print("He is: " + str(self.age) + " y.o.")
print("His weight: " + str(self.weight) + " Kg")
def __init__(self, name, age, weight, animal_type):
self.name = name
self.age = age
self.weight = weight
self.animal_type = animal_type
print("A new animal has been created!")
self.__chars()
# public methods
def eat(self):
print("The animal eat!")
def drink(self):
print("The animal drink!")
def play(self):
print("The animal plays!")
def get_name(self):
return self.name
# public destructor
def __del__(self):
print('n' + self.name + "has died :(")

# Child classes
class dog(animal):
__dlevel = None # dangerous level, it can be 0, 1 or more, it's private

# private methods
def set_dlevel(self, dlevel):
self.__dlevel = dlevel
print("Dangerous level set!")

def get_dlevel(self):
# CHeck if the level is define
if not self.__dlevel:
print("Dog dangerous level not found")
sys.exit(1)
# if is equal or more than 1 is dangerous
if int(self.__dlevel) >= 1:
print("The dog is dangerous, be careful while playing with him!")
# otherwise it's a quiet dog
elif int(self.__dlevel) <= 0:
print("The dog isn't dangerous")





We would need to see the classes you defined, but what has most likely happened is that dog.name is a function, which is why, instead of printing the name of the dog, it prints the function details.
– Jono 2906
Jun 28 at 22:43


dog.name





@Jono2906 i've added the classes code
– icebit
Jun 28 at 22:47





You have nothing in that code that sets the name.
– dawg
Jun 28 at 22:53





I ran the code you provided, and it seemed to work -- I saw nothing about <bound method> in the output.
– Jono 2906
Jun 28 at 22:53


<bound method>





Yes, in the constructor: self.name = name
– icebit
Jun 28 at 22:55


self.name = name




1 Answer
1



I figured out the real problem: i was printing the function, not calling it.
So instead i've used


def func(animal):
print(animal.get_name())

def main():
new_dog = dog("max", 14, 11, "dog")
func(new_dog)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV