简体   繁体   中英

How to define a callable object into another python class

I have defined a Python class. I am trying to get that result object into another class. I am not able to do it till now while I have tried and read a lot through the internet. Is there anyone who can kindly help me?

The idea is to stack the arrays result from the layer definition at it first.

Thanks for your time.

    class Lit_dim():
        num_units = 0 #class variables #Class variable to share with all instances
        """
        Create a new layer grid for the BHE simulation model

        """
        def __init__(self, x, y, z): #Constructor
            """
            Definition of variables for grid creation with the layer definition
            """
            self.x = x   
            self.y = y  #Instance variables shared to each instance
            self.z = z
            self.units =[] #Creating the units list to an specific unit
            Lit_dim.num_units += 1

        def unit_index(self, unit_index):
            self.unit_index = unit_index
            self.layer = np.full((self.x, self.y), self.unit_index)
            self.layer = np.repeat(self.layer, self.z, axis=0)
            # return (self.layer)

        def displayCount(self):
            print ("Total number of units created: ", Lit_dim.num_units)

The idea behind is to use the object from this previous class into a new class. However, when I am trying to do something with the object is telling me that the object is not callable.

class model_creation(Lit_dim):

    def __init__(self):
        print ("Test")
        unit_add = Lit_dim()
        print (unit_add)

If your idea is to use the object from this previous class into a new class, you just call Lit_dim, without passing It as as argument.

class Lit_dim():
        num_units = 0 #class variables #Class variable to share with all instances
        """
        Create a new layer grid for the BHE simulation model

        """
        def __init__(self, x, y, z): #Constructor
            """
            Definition of variables for grid creation with the layer definition
            """
            self.x = x   
            self.y = y  #Instance variables shared to each instance
            self.z = z
            self.units =[] #Creating the units list to an specific unit
            Lit_dim.num_units += 1

        def unit_index(self, unit_index):
            self.unit_index = unit_index
            self.layer = np.full((self.x, self.y), self.unit_index)
            self.layer = np.repeat(self.layer, self.z, axis=0)
            # return (self.layer)

        def displayCount(self):
            print ("Total number of units created: ", Lit_dim.num_units)

class model_creation():

    def __init__(self):
        print ("Test")
        unit_add = Lit_dim(1, 2, 3)
        print (unit_add, unit_add.x, unit_add.y, unit_add.z)

def main():
    model_creation()
main()

Output:

Test
<__main__.Lit_dim object at 0x10a59f198> 1 2 3

You need to specify what variable you are trying to access:

def __init__(self):
    print ("Test")
    unit_add = Lit_dim()
    print (unit_add.<variable name here>)

You first make a object out of a class like you did:

unit_add = Lit_dim()

But then you need to access specific variables in that object

print(unit_add.<variable name here>)

You have to define inside of the class what will happen when you call a object of it.

def __repr__(self):
    return "whatever atributes you wish"

There is a special case for print.

def __str__(self):
    return "Print this"

>>>> t = Lit_Dim()
>>>> t
whatever atributes you wish
>>>> print(t)
Print this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM