constructor

Javascript `this` vs Python `self` Constructors

Javascript `this` vs Python `self` Constructors Question: Javascript Constructor + create objects example //Constructor function Course(title,instructor,level,published,views){ this.title = title; this.instructor = instructor; this.level = level; this.published = published; this.views = views; this.updateViews = function() { return ++this.views; } } //Create Objects var a = new Course(“A title”, “A instructor”, 1, true, 0); var b = …

Total answers: 2

Multiple constructors: the Pythonic way?

Multiple constructors: the Pythonic way? Question: I have a container class that holds data. When the container is created, there are different methods to pass data. Pass a file which contains the data Pass the data directly via arguments Don’t pass data; just create an empty container In Java, I would create three constructors. Here’s …

Total answers: 7

Change default constructor argument value (inherited from parent class) in subclass

Change default constructor argument value (inherited from parent class) in subclass Question: I have a Parent class with a default value for the attribute arg2. I want to create a subclass Child which has a different default value for the same attribute. I need to use *args and **kwargs in Child. I tried the following, …

Total answers: 3

Python __enter__ / __exit__ vs __init__ (or __new__) / __del__

Python __enter__ / __exit__ vs __init__ (or __new__) / __del__ Question: I have searched and I’m unable to come up with any good reason to use python’s __enter__ /__exit__ rather than __init__ (or __new__ ?) / __del__ . I understand that __enter__ / __exit__ are intended for use with the with statement as context managers, …

Total answers: 3

Instance variables in methods outside the constructor (Python) — why and how?

Instance variables in methods outside the constructor (Python) — why and how? Question: My questions concern instance variables that are initialized in methods outside the class constructor. This is for Python. I’ll first state what I understand: Classes may define a constructor, and it may also define other methods. Instance variables are generally defined/initialized within …

Total answers: 7

Python property does not use setter when setting the value in the constructor

Python property does not use setter when setting the value in the constructor Question: I have a class with a constructor and a couple of properties class Foo(object): def __init__(self, value1): self._value1 = value1 @property def value1(self): return self._value1 @property.setter def value1(self, value): assert value == 1 self._value1 = value Now when I set value1 …

Total answers: 2

How to stub time.sleep() in Python unit testing

How to stub time.sleep() in Python unit testing Question: I want to make a stub to prevent time.sleep(..) to sleep to improve the unit test execution time. What I have is: import time as orgtime class time(orgtime): ”’Stub for time.”’ _sleep_speed_factor = 1.0 @staticmethod def _set_sleep_speed_factor(sleep_speed_factor): ”’Sets sleep speed.”’ time._sleep_speed_factor = sleep_speed_factor @staticmethod def sleep(duration): …

Total answers: 6

__init__() missing 1 required positional argument

__init__() missing 1 required positional argument Question: I am trying to learn Python. This is a really simple code. All I am trying to do here is to call a class’s constructor. Initialize some variables there and print that variable. But it is giving me an error. It is saying: missing 1 required positional argument …

Total answers: 6

How to call Base Class's __init__ method from the child class?

How to call Base Class's __init__ method from the child class? Question: If I have a python class as: class BaseClass(object): #code and the init function of the base class And then I define a child class such as: class ChildClass(BaseClass): #here I want to call the init function of the base class If the …

Total answers: 4

Haskell equivalent of data constructors in Python?

Haskell equivalent of data constructors in Python? Question: In Haskell, I can define a binary tree as follows: data Bint a = Leaf a | Branch a (Bint a) (Bint a) then I can some operations on it as follows: height (Leaf a) = 1 height (Branch a l r) = 1 + (max (height …

Total answers: 5