-1

So I wrote a code for returning a list of objects with dimensions (5, 3, 3, 3) in python 3.5. Now my problem is that whenever I try to print the returned value, it prints the addresses of 5 separate 3D lists instead of the list as a whole. Even the type of returned value shows up as list. What exactly is the problem here?

Here is my initializing and returning function.

class layer(object):

def __init__(self, inputDimensions, channels, padding, stride, layerInput):
    self.inputDimensions = inputDimensions
    self.channels = channels
    self.padding = padding
    self.stride = stride
    self.layerInput = layerInput

def getLayerInfo(self):
    return self.inputDimensions, self.channels, self.padding, self.stride

def getLayerInput(self):
    return self.layerInput

def getLayerFilterInfo(self):
    return self.filterDimensions, self.numberOfFilters

def getLayerFilters(self):
    return self.filters

def initializeFilter(self, filterDimensions, numberOfFilters):
    self.filterDimensions = filterDimensions
    self.numberOfFilters = numberOfFilters
    self.filters = []
    for i in range(0, self.numberOfFilters):
        fil = filters(self.filterDimensions)
        self.filters.append(fil)

Here is my filter class.

class filters(object):

    def __init__(self, dimensions):
        self.dimensions = dimensions
        self.fil = np.random.random_sample(self.dimensions)

Here is a sample of input and output.

In [11]: l.getLayerFilters()

Out[11]: 
[<__main__.filters at 0xb195a90>,
 <__main__.filters at 0xb1cb588>,
 <__main__.filters at 0xb1cb320>,
 <__main__.filters at 0xb1cb5c0>,
 <__main__.filters at 0xb1cbba8>]


In [12]: type(l.getLayerFilters())
Out[12]: list
DuttaA
  • 903
  • 4
  • 10
  • 24
  • 1
    That is the default `__repr__` inherited from `object`... what did you *expect* it to print. As an aside, `list` objects don't have dimensions. – juanpa.arrivillaga May 22 '18 at 05:14
  • 1
    Possible duplicate of [How to print a class or objects of class using print()?](https://stackoverflow.com/questions/1535327/how-to-print-a-class-or-objects-of-class-using-print) – OneCricketeer May 22 '18 at 05:14
  • @cricket_007 this is not a duplicate since i dont know the nature of the problem...nor can I understand the duplicate question – DuttaA May 22 '18 at 05:15
  • @juanpa.arrivillaga but then it shows up as list type – DuttaA May 22 '18 at 05:16
  • Definitely a duplicate... You need to tell Python **how to print** your object by implementing a function other than the constructor called `__repr__` – OneCricketeer May 22 '18 at 05:17
  • The list is irrelevant to the problem... `type(l.getLayerFilters()[0])` is a filter object – OneCricketeer May 22 '18 at 05:19
  • @cricket_007 the question is totally irrelevant to my question...i did not ask for the __str__ method which gives some definition of the object....I wanted to know how to return data members – DuttaA May 22 '18 at 05:21
  • @cricket_007 also the other data members are getting printed just fine – DuttaA May 22 '18 at 05:22
  • Maybe try `[f.dimensions for f in l.getLayerFilters()]`? – OneCricketeer May 22 '18 at 05:23
  • Being printed where? Not seeing any actual print statements in that code – OneCricketeer May 22 '18 at 05:23
  • @cricket_007 i printed the full class now...the other data members of this class are being returned fine – DuttaA May 22 '18 at 05:26
  • Your question is *What exactly is the problem here?*... The problem being you did not implement `__repr__`, therefore you are seeing "the addresses" of your `filters` classes within a list... That list being irrelevant because printing a single element from it would show you that same "address" output. Make sense now? – OneCricketeer May 22 '18 at 05:26
  • @cricket_007 everywhere in my code I am returning a value...so where are the addresses coming from? – DuttaA May 22 '18 at 05:28
  • How about this - just try to implement that `__repr__` function and repeat the code in your question rather than trying to argue... If your question gets closed as a duplicate, I'll reopen. Sound fair? – OneCricketeer May 22 '18 at 05:29
  • @cricket_007 I am looking for a general answer...Not only i dont get the answer of the duplicate question, whatever little i get i dont understand how is it going to help me – DuttaA May 22 '18 at 05:32
  • @cricket_007 https://stackoverflow.com/questions/1984162/purpose-of-pythons-repr?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa according to this __repr__ gives a printable representation of the object....I only want to return values thats all...what if I want to return values other than those lists? I cannot have 2 __repr__ methods – DuttaA May 22 '18 at 05:36
  • I'm sorry, but what are you not getting about it? Any answer shown on your post here would have the same details as answered there... You have `class filters(object)` without any defined function `def __repr__(self)`... Therefore, as per class inheritance, you're seeing the default definition of the object output which is `<__main__.filters at 0xb195a90>`, or as the other post says using `Foobar` as only an example – OneCricketeer May 22 '18 at 05:37
  • 1
    @DuttaA: This is a duplicate (see link above) because it has the same reason - you use default representation, but want custom representation. What is to do? Implement method __str()__ in the classes "layer" and "filters". Then your own text representation instead of the default one will be used. – mentallurg May 22 '18 at 05:40
  • @DuttaA: Override method \_\_str()\_\_ in both your classes. – mentallurg May 22 '18 at 05:43
  • @mentallurg have you even read the previous comments? – DuttaA May 22 '18 at 05:49

2 Answers2

1

In short: Instead of doing this:

fil = filters(self.filterDimensions)
self.filters.append(fil)

you can probably achieve what you want if you do this:

fil = filters(self.filterDimensions)
self.filters += fil.fil

Not sure, it depends on what those filters are supposed to be and how you want to put them together and in your result. Then again, it seems possible to get rid of the filters class altogether.

As it is, you're creating instances of your filters class, then append them to a list. What you get when you print that list is, as expected and seen in OP, a list of objects. Those objects ofc don't represent themselves as lists, they are all general objects with a default string representation that only shows the class that object "comes from" and the address in memory to remind you that it's indeed an object and not a class. To make your class aware of that it's supposed to be some sort of list and to make it behave that way, you could implement the __repr__ method like so:

def __repr__(self):
    return "<Filters: {}>".format(self.fil)

Put that in your filters class and any print of it should now show you the list inside of it (or its representation). Improvable, still.

BTW: Please consider renaming your class to Filters or so. Upper camel case is what PEP8 suggests for classes.

Jeronimo
  • 2,268
  • 2
  • 13
  • 28
  • Thanks man,,,appreciate the help...just a question, before the beginning of every 1 out of 5 list there is this name you provided ""...I don't want it to be there since I am concerned with the pure list...how will i remove it? – DuttaA May 22 '18 at 06:05
  • Then you can return `str(self.fil)` from `__repr__` instead. It's just important that what it returns is a string, so returning the list directly doesn't work. Be aware though that this can become confusing, since you now cannot distinguish anymore if it's a true `list` or one of your objects. – Jeronimo May 22 '18 at 06:08
1

I wanted to know how to return data members

You need to access them

filters = l.getLayerFilters()
for f in filters:
    print(f.dimensions, f.fil)

whenever I try to print the returned value, it prints the addresses

You never told Python how else it should print your object. Just because those fields are there, it will not automatically show you them.

As attempted to discuss with you in the comments, you will need to override that output behavior yourself with a new function that returns a single human-readable representation of your class

As an example

class filters(object):

    def __init__(self, dimensions):
        self.dimensions = dimensions
        self.fil = np.random.random_sample(self.dimensions)

    def __repr__(self):
        return "dimensions: {}\nfil: {}".format(self.dimensions, self.fil)

Now, try it again

Some more reading

https://stackoverflow.com/a/2626364/2308683

Understanding repr( ) function in Python

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Now that was the answer I was looking for..thanks...only problem it is showing something called dimensions along with the original list and also clearing up the interactive consoles previous history – DuttaA May 22 '18 at 05:52
  • Oops. I missed the `\n` for newline... Feel free to change the output format how you see fit – OneCricketeer May 22 '18 at 05:55
  • "something called dimensions"? Well, that is what you named your parameter – OneCricketeer May 22 '18 at 05:56
  • Actually I am not familiar with line formatting in python..Is it the same as in C? – DuttaA May 22 '18 at 05:56