0

I am learning Python and classes and i encountered a challenge that i need help with.

When I try to pass data from one class method to another class method i run into an error that tells me it misses a positional argument

example:

class Student:
    def __init__(self, name):
        self.name = name

    def pass_to_dl(self):
       DataLayer.Datalayer.create_dict_entry(self.name)



class Datalayer:
   def __init__(self):
        self.dict = {}

   def create_dict_entry(self, name):
       new_name = {"name":name}
       self.dict.update(new_name)


new_student = Student("some_name")

The error I get is:

TypeError: create_new_student() missing 1 required positional argument:

i assume it is because create_dict_entry expects two arguments and I am sending only one

now i can overcome this if i do two things

  1. make the create_dict_entry in DataLayer static
  2. move the dict outside of the DataLayer class but truthfully that does not seem like the right approach to me i think

i would love to hear your view on this matter

thnx

ps. i dont want student to inherit from DataLayer as i consider DataLayer a non related class that just holds data that i acquire form classes such as Student, Teacher etc

Eelco
  • 49
  • 1
  • 3
  • 2
    please make your code valid. E.g. there should be `:` after `class Student` – Jan Stránský Jul 17 '20 at 11:39
  • The thing you call a 'class method' is just a 'method'. There is such thing as 'class method', you can read about it here: https://stackoverflow.com/questions/12179271/meaning-of-classmethod-and-staticmethod-for-beginner – Rugnar Jul 17 '20 at 11:42

1 Answers1

0

Not sure it's the way you want to go, but if I understand correctly what you're trying to do, I think you want to use inheritance.

class Datalayer:
    def __init__(self):
        self.dict = {}

    def create_dict_entry(self, name):
        new_name = {"name":name}
        self.dict[self.name] = new_name

class Student(Datalayer):
    def __init__(self, name):
        super().__init__()
        self.name = name

    def pass_to_dl(self):
        self.create_dict_entry(self.name)

So in this example, Student is your "child class" (that's why you pass Datalayer as its argument). In your __init__, you want to call super(Student, self).__init__() (or just super().__init__() to instantiate your "parent class", Datalayer so you can access trigger its __init__ function.

You may want to read some other examples to understand it properly (https://realpython.com/python-super/).

RomainM
  • 149
  • 7
  • thnx, i am not sure why, but i wanted to avoid making Student a child class of Datalayer. in my mind datalayer is a datastorage class that resides in paralel. – Eelco Jul 17 '20 at 12:06
  • Then maybe a simple side function is enough ? To me, it looks kind of weird to have several classes that you need to instantiate : it seems simpler, and I guess more "pythonic" to use inheritance. – RomainM Jul 17 '20 at 14:01
  • i understand your comment, but i am not trying to create an instance in the Datalayer, i just want to call a method with DataLayer that stores data within the class itself. Makes sense, or am i going in the wrong direction? – Eelco Jul 17 '20 at 16:20
  • 1
    You need to instantiate your class if you want to use the dict you have as one of its property (self.dict in the Datalayer class). – RomainM Jul 17 '20 at 20:25