0

In javascript, one can store a function class with function parameters inside an object like so:

const myFunctionClass = function(){
  this.propertyFunction = function myFunc(){
    console.log('hi')
  }
}

const initFunctionClass = new myFunctionClass()
const obj = {}
obj["myFunctionClass"] = initFunctionClass
console.log(obj)

/* Output
{
  myFunctionClass: myFunctionClass { propertyFunction: [Function: myFunc] }
}
*/

The javascript code allows to pass an initiated class around and use its functions by other modules when imported.

Is there a similar pattern in python? If so, what is the pattern? If not, could someone help clarify how I should be thinking about this in python (maybe I'm missing something obvious), perhaps with some code examples?

import os
from os import walk


class Core:
    def load_adapters(self, path):
        api_adapters = {}
        for folder in os.scandir(path):
            api_adapter = folder.name
            file = next(walk(path + '/' + folder.name), (None, None, []))[2]  # [] if no file
            
            # I want to import each found file, which will contain a class,
            # then initialize the classes and validate them with unit testing
            from api_adapters[api_adapter][file] import api_adapter # this code relates to the question
        return api_adapters
Dshiz
  • 3,099
  • 3
  • 26
  • 53
  • 1
    Have you tried using classes in python? – Konrad Oct 10 '22 at 20:17
  • Yes, but my experience with them is just from today. I'll post my code attempt. – Dshiz Oct 10 '22 at 20:19
  • 3
    I can't understand the problem because the terminology used does not make any sense, and because I know that Javascript's classes work strangely (very differently from Python's). Code translation requests are [usually off topic](https://meta.stackoverflow.com/questions/265825). Please try to use English sentences to explain exactly what the Python code needs to do. If the question is really "at an introductory level, how do I use classes in Python?", then consider following a tutorial instead; the question is most likely still too unfocused for this format. – Karl Knechtel Oct 10 '22 at 20:19
  • Thanks @KarlKnechtel - That actually makes sense to me, and gives me a direction. – Dshiz Oct 10 '22 at 20:21
  • By the way, `function class` is a functional way to write classes in javascript. It made more sense to me at the time I was learning javascript, so that's what I've used. – Dshiz Oct 10 '22 at 20:22
  • This syntax was replaced in JavaScript with `class` keyword because it was so confusing. Don't expect anything like that in other modern languages. – Konrad Oct 10 '22 at 20:26
  • This does not look like a common pattern in JavaScript, either. Functions are usually assigned to properties of the prototype. – Barmar Oct 10 '22 at 20:29
  • @Barmar You are correct. However, it has enabled rapid development for things I've worked on. I got the idea from [bugout](https://github.com/chr15m/bugout) and it stuck. – Dshiz Oct 10 '22 at 20:33
  • I think I was able to find the answer to my question [here](https://stackoverflow.com/a/32805284/1551027) – Dshiz Oct 10 '22 at 20:36

0 Answers0