-2

Perhaps this is not the right way to approach the class structure however, I was trying to store a dataset within __init__ ad then call them in another function to read the first few results from head, however I do not get a dataframe output.

for example:

import faraway.datasets.pima

pima = faraway.datasets.pima.load()
#print(pima.head())

class automate_lm:

    def __init__(self, data):
        __dataset__ = data
    
    def __print__(self):
        __p__ = self.__dataset__.head()
        return(__p__)

if __name__ == '__main__':
    test=automate_lm(pima)
    print(test.__print__)

Gives:

<bound method automate_lm.__print__ of <__main__.automate_lm object at 0x7fae1014b670>>

Expected output:

   pregnant  glucose  diastolic  triceps  insulin   bmi  diabetes  age  test
0         6      148         72       35        0  33.6     0.627   50     1
1         1       85         66       29        0  26.6     0.351   31     0
2         8      183         64        0        0  23.3     0.672   32     1
3         1       89         66       23       94  28.1     0.167   21     0
4         0      137         40       35      168  43.1     2.288   33     1
Dollar X
  • 73
  • 5
  • 1
    You are not calling the `__print__` function. (Why are you putting `__` around everything, by the way?) – timgeb Jun 17 '22 at 12:58
  • @timgeb no specific reason besides having seen `__` used a lot in class structures to name things. – Dollar X Jun 17 '22 at 13:02
  • Alright, my advice would be to not invent your own`__dunder__` attributes. This format should be reserved for "magic methods", i.e. the ones defined in the Python data model, such as `__init__`, `__str__`, `__getitem__` etc. – timgeb Jun 17 '22 at 13:04
  • 1
    @DollarX: That's the *opposite* of what you should do, names with `__` used as both prefix and suffix are reserved to the language and can be repurposed at any time even if it breaks your code, you *only* use [the documented ones](https://docs.python.org/3/reference/datamodel.html#special-method-names) for their documented purpose. If you're just trying to say "these attributes are for internal usage by the class only", use a single leading underscore (e.g. `self._dataset`), if they're fully private even from subclasses, use two leading underscores (but no trailing, e.g. `self.__dataset`). – ShadowRanger Jun 17 '22 at 13:05
  • 3
    There are a sufficiently large number of mistakes in both correctness and style here that I'm going to tell you: Stop what you're doing, right now, and run through a complete Python tutorial (or take a class). What you're engaged in right now is the definition of [cargo cult programming](https://en.wikipedia.org/wiki/Cargo_cult_programming), you're reusing code snippets and design concepts you've seen but do not understand, and the end result does not function remotely like anything you're trying to mimic. StackOverflow cannot correct all these misconceptions a question at a time. – ShadowRanger Jun 17 '22 at 13:09

3 Answers3

1

You forget to put self before 'dataset'

_dataset (pseudo private attribute) sound better thant __dataset__

Underscore vs Double underscore with variables and methods

a more pythonic way, will look like this

class automate_lm:

    def __init__(self, data):
        self._dataset = data
    
    def __str__(self):
        return self._dataset.__str__()

    def __repr__(self):
        return self._dataset.__repr__()

if __name__ == '__main__':
    test = automate_lm(pima)
    print(test)
Mechanic Pig
  • 6,756
  • 3
  • 10
  • 31
Faulheit
  • 116
  • 7
1

Okay, I see you have two Simple, But fatal mistakes in your code.

I'll try to explain this to you by reproducing your code with a dummy dataframe.

import pandas as pd

df = pd.DataFrame([[1,2,3],[4,5,6],[7,8,9]],columns = ["a","b","c"])

class automate_lm:

    def __init__(self, data):
        self.__dataset__ = data # First correction
    
    def __print__(self):
        __p__ = self.__dataset__.head() # First correction
        return(__p__)

if __name__ == '__main__':
    test=automate_lm(df)
    print(test.__print__()) # Second correction

First correction

To a variable be able to accessed via all the function inside a class, it should be assigned with and called with self. at the beginning. You have done this mistake at two places. When assigning and when it was called.

Second correction

When you want to print a result of a function, You should call the function with (*args,**kwargs) at the end. But you have just called its name without even parentheses. Just point to the location of the function in the memory. Which will give you <bound method automate_lm.__print__ of <__main__.automate_lm object at 0x7fae1014b670>>

Kavindu Ravishka
  • 711
  • 4
  • 11
0

You should add self. in __init__ like so

    def __init__(self, data):
        self.__dataset__ = data

Then you can use it in other methods.

    def __print__(self):
        __p__ = self.__dataset__.head()
        return(__p__)

You also have mistake later on when calling __print__, you need to call it with () like:

print(test.__print__())
lutrarutra
  • 180
  • 1
  • 10