0

I'm new for python, I try to create class that put 3 values (channel, mDM, crossec) to gennerated data from .dat to be like (x, y) value for make chi-square fit. Now I try to print to see data but it doesn't show

my class code file name DMM.py. I try this

from DMM import cal

channel = str(input("Enter channel "))
mDM = int(input("Enter mDM "))
crossec = int(input("Enter crossecn "))

y = cal(channel, mDM, crossec)

print(y)

and it print out

Enter channel t
Enter mDM 150
Enter crossec 1
<DMM.cal object at 0x00FAEBD0>

I try to read from the internet but still not understand

fnames = 'AtProductionNoEW_gammas.dat'
data = np.genfromtxt(fnames, names=True, dtype=None)
mass_vals = data["mDM"]

class cal():

    def __init__(self, channel, mDM, crossec):

        self.channel = channel
        self.mDM = mDM
        self.crossec = crossec

        index = np.where(np.abs(mass_vals - self.mDM)/mass_vals < 0.0001)

        xvals = data["Log10x"][index]

        fluxs = data[self.channel][index]/xvals

        loadspec = interp1d(xvals, fluxs)

        def dNdx(x):
            fluxval = loadspec(x)
            if (fluxval<0):
                return 0
            else:
                return loadspec(x)*/(8*pi*self.mDM**2)*self.crossec

I want output be like

1, 2
2, 5
7, 8
9, 15
11, 25
...
  • 1
    When printing an object, that object is first converted to a string, so that it can be printed. You can define how an object is converted to a string by implementing the `__str__` method on the class. If you don't (as you didn't), the default implementation of `__str__` will return something like ``. – zvone Sep 07 '19 at 16:28
  • Possible duplicate of [How to print instances of a class using print()?](https://stackoverflow.com/questions/1535327/how-to-print-instances-of-a-class-using-print) – Ondrej K. Sep 07 '19 at 17:14

2 Answers2

0

y is your cal object and indeed the print(y) prints the object string representation.

I believe you want to print the output from a method that you have to add to your class. Currently you don't have a method but an inner function dNdx().

Lior Cohen
  • 5,570
  • 2
  • 14
  • 30
0

You have to override the __str__ function from your class with the data that you want represented when you print your object.

For example:

class MyClass:
    def __init(id):
        self.id = id

    def __str__(self):
        return 'This is the text I want printed with the value {}".format(self.id)
NicoT
  • 341
  • 2
  • 11