[Beginner Python]Issues trying to access a function within a class

I am trying to run a simple budget program and I am a complete beginner with classes in python. I am trying to run everything within the class so I can call back to specific functions. The problem is I can’t get the functions to work, I keep returning a NameError when I try to… Read More [Beginner Python]Issues trying to access a function within a class

Are class variables accessed via self used as iterables good practice/pythonic?

Is it appropriate to use a class variable accessed with self as an iterable in a loop? The following python code is valid and produces the expected result ("1, 2, 3") but it feels wrong somehow: class myClass(): foo = None def myfunc(self): for self.foo in [1, 2, 3]: print(self.foo) return myClass().myfunc() I have not… Read More Are class variables accessed via self used as iterables good practice/pythonic?

Initialize class more efficiently in Python

I have this code in which I initialize a class (Adapter) by the name I get from the request. It seems to be a bit clumsy, and I’m sure there’s a better/cleaner way of doing it. from adapters.gofirst_adapter import GoFirstAdapter from adapters.spicejet_adapter import SpiceJetAdapter from adapters.airasia_adapter import AirAsiaAdapter class Adapter(): def __init__(self, adapter_name): if(adapter_name==’goFirst’): gofirst_adapter… Read More Initialize class more efficiently in Python

Python: How to get all the parent classes and function names in one array within that function

What I tried: import inspect class ClassA: class ClassB: def FunctionC(): print(inspect.stack()[0][3]) # returns ‘FunctionC’ ClassA.ClassB.FunctionC() This only returns the current function name, what I need is all the parent classes and function names inside one array. I mean something like this: output = [‘ClassA’, ‘ClassB’, ‘FunctionC’] Is it possible? >Solution : Looks like inspect.stack()… Read More Python: How to get all the parent classes and function names in one array within that function

python oop class attribute returns None when set

I’m trying to learn object orientated programming and when setting an attribute why does it return None? Any tips on writing better code also appreciated… This is my class: BEANS = ["black", "pinto"] RICES = ["brown", "white"] MEATS = ["chicken", "pork", "steak", "tofu"] class Burrito: def __init__(self, meat, to_go, rice, beans, extra_meat=False, guacamole=False, cheese=False, pico=False,… Read More python oop class attribute returns None when set

self.__class__ in parent class' method is missing arguments

A package I am using contains the class A. I want my new class B to inherit from this class. Minimal example: class A(): def __init__(self,name): self.name = name def my_method(self): return self.__class__(name = self.name) class B(A): def __init__(self, value, name): self.value = value super().__init__(name) B_instance = B(value = 5, name = "Bob") B_instance.my_method() Calling… Read More self.__class__ in parent class' method is missing arguments

Python Class – Can't find out how to use method return value within another method

So this is the class i’m testing: class Test: def find_string(self, string): self.string = string return string.find(string) def add_string(self, string): found = self.find_string(‘bar’) if found == -1: string = string + ‘ bar’ return string Here is my setup: test_string = ‘foo’ Test1 = Test() new_string = Test1.add_string(string) Results Expected result: foo bar Result: foo… Read More Python Class – Can't find out how to use method return value within another method