initialization

Best way to initialize and fill an numpy array?

Best way to initialize and fill an numpy array? Question: I want to initialize and fill a numpy array. What is the best way? This works as I expect: >>> import numpy as np >>> np.empty(3) array([ -1.28822975e-231, -1.73060252e-077, 2.23946712e-314]) But this doesn’t: >>> np.empty(3).fill(np.nan) >>> Nothing? >>> type(np.empty(3)) <type ‘numpy.ndarray’> It seems to me …

Total answers: 5

Initializing a dictionary in python with a key value and no corresponding values

Initializing a dictionary in python with a key value and no corresponding values Question: I was wondering if there was a way to initialize a dictionary in python with keys but no corresponding values until I set them. Such as: Definition = {‘apple’: , ‘ball’: } and then later i can set them: Definition[key] = …

Total answers: 8

Initialize list with same bool value

Initialize list with same bool value Question: Is it possible without loops initialize all list values to some bool? For example I want to have a list of N elements all False. Asked By: Alecs || Source Answers: You can do it like this: – >>> [False] * 10 [False, False, False, False, False, False, …

Total answers: 3

How to extend Python class init

How to extend Python class init Question: I have created a base class: class Thing(): def __init__(self, name): self.name = name I want to extend the class and add to the init method so the that SubThing has both a name and a time property. How do I do it? class SubThing(Thing): # something here …

Total answers: 3

__init__ as a constructor?

__init__ as a constructor? Question: Dive into Python – It would be tempting but incorrect to call this the constructor of the class. It’s tempting, because it looks like a constructor (by convention, __init__ is the first method defined for the class), acts like one (it’s the first piece of code executed in a newly …

Total answers: 6

Set attributes from dictionary in python

Set attributes from dictionary in python Question: Is it possible to create an object from a dictionary in python in such a way that each key is an attribute of that object? Something like this: d = { ‘name’: ‘Oscar’, ‘lastName’: ‘Reyes’, ‘age’:32 } e = Employee(d) print e.name # Oscar print e.age + 10 …

Total answers: 8

Python – Initializing Multiple Lists/Line

Python – Initializing Multiple Lists/Line Question: This is terribly ugly: psData = [] nsData = [] msData = [] ckData = [] mAData = [] RData = [] pData = [] Is there a way to declare these variables on a single line? Asked By: thenickname || Source Answers: alist, blist, clist, dlist, elist = …

Total answers: 7

using __init__.py

using __init__.py Question: I am having difficulty understanding the usage scenarios or design goals of python’s __init__.py files in my projects. Assume that I have ‘model’ directory (refers as a package) which contains the following files __init__.py meta.py solrmodel.py mongomodel.py samodel.py I found two ways of using __init__.py: I have common a definition which needs …

Total answers: 2

How to create a class instance without calling initializer?

How to create a class instance without calling initializer? Question: Is there any way to avoid calling __init__ on a class while initializing it, such as from a class method? I am trying to create a case and punctuation insensitive string class in Python used for efficient comparison purposes but am having trouble creating a …

Total answers: 4

Python Class Members Initialization

Python Class Members Initialization Question: I have just recently battled a bug in Python. It was one of those silly newbie bugs, but it got me thinking about the mechanisms of Python (I’m a long time C++ programmer, new to Python). I will lay out the buggy code and explain what I did to fix …

Total answers: 5