init

Call of class in python with attributes

Call of class in python with attributes Question: I made a typo with the __init__() in line 23, but my program runs only with this failure and shows the right result. Could some experienced OOP expert help me please. If I correct this tripple underscore ___init__() to the correct on __init__(file_path) I get this ERROR …

Total answers: 1

How do I add a value to a parameter in an init function in python

How do I add a value to a parameter in an init function in python Question: Would it work if I added these values in this way or do I need to add double quotes to the values like "ENGR", "ARTS", …etc"? Also am I supposed to use curly brackets or square brackets? def __init__(self, …

Total answers: 1

How can I write cleanly __init__ method in python?

How can I write cleanly __init__ method in python? Question: I want write cleanly init method in python. my init method is very long and dirty. I don’t know how can clean it. and its my dirty code: class Detect: """Class to show frames""" def __init__(self, main_target, target_object, camera, sms_class, qr_code_class, cuda="CPU"): self.main_target = main_target …

Total answers: 4

__init__.py can't find local modules

__init__.py can't find local modules Question: Borrowing a simplified example at http://pythoncentral.io/how-to-create-a-python-package/ I have an analogous file structure as follows, where Mammals.py and Birds.py define classes with the same names: Project/ Animals/ __init__.py Mammals.py Birds.py When running an ipython interpreter within the Project/ directory and with __init__.py being empty, the following works: from Animals.Mammals import …

Total answers: 2

Python __init__ return failure to create

Python __init__ return failure to create Question: First off, I know that the __init__() function of a class in Python cannot return a value, so sadly this option is unavailable. Due to the structure of my code, it makes sense to have data assertions (and prompts for the user to give information) inside the __init__ …

Total answers: 4

How to fix "Attempted relative import in non-package" even with __init__.py

How to fix "Attempted relative import in non-package" even with __init__.py Question: I’m trying to follow PEP 328, with the following directory structure: pkg/ __init__.py components/ core.py __init__.py tests/ core_test.py __init__.py In core_test.py I have the following import statement from ..components.core import GameLoopEvents However, when I run, I get the following error: tests$ python core_test.py …

Total answers: 22

Python: Inherit the superclass __init__

Python: Inherit the superclass __init__ Question: I have a base class with a lot of __init__ arguments: class BaseClass(object): def __init__(self, a, b, c, d, e, f, …): self._a=a+b self._b=b if b else a … All the inheriting classes should run __init__ method of the base class. I can write a __init__() method in each …

Total answers: 7

Inheritance and init method in Python

Inheritance and init method in Python Question: I’m begginer of python. I can’t understand inheritance and __init__(). class Num: def __init__(self,num): self.n1 = num class Num2(Num): def show(self): print self.n1 mynumber = Num2(8) mynumber.show() RESULT: 8 This is OK. But I replace Num2 with class Num2(Num): def __init__(self,num): self.n2 = num*2 def show(self): print self.n1,self.n2 …

Total answers: 4

Using module's own objects in __main__.py

Using module's own objects in __main__.py Question: I’m trying to access a module’s data from inside its __main__.py. The structure is as follows: mymod/ __init__.py __main__.py Now, if I expose a variable in __init__.py like this: __all__ = [‘foo’] foo = {‘bar’: ‘baz’} How can I access foo from __main__.py? Asked By: sharvey || Source …

Total answers: 6

How to return a value from __init__ in Python?

How to return a value from __init__ in Python? Question: I have a class with an __init__ function. How can I return an integer value from this function when an object is created? I wrote a program, where __init__ does command line parsing and I need to have some value set. Is it OK set …

Total answers: 13