Questions tagged [python-object]
102 questions
14
votes
6 answers
Can I declare Python class fields outside the constructor method?
I am absolutely new to Python (I came from Java) and I have the following doubts about class fields.
Considering code like this:
class Toy():
def __init__(self, color, age):
self.color = color
self.age = age
action_figure =…

AndreaNobili
- 40,955
- 107
- 324
- 596
13
votes
1 answer
Setting a class __name__ declaratively
Why can't you override a class name declaratively, e.g. to use a class name which is not a valid identifier?
>>> class Potato:
... __name__ = 'not Potato'
...
>>> Potato.__name__ # doesn't stick
'Potato'
>>> Potato().__name__ # .. but…

wim
- 338,267
- 99
- 616
- 750
11
votes
2 answers
'super' object has no attribute '__eq__'
When I try to override the magic method __eq__, and use super to access the base method found in object, I get an error. There's no way this is a bug, but it sure feels like one:
class A(object):
def __eq__(self, other):
return super(A,…

rmorshea
- 832
- 1
- 7
- 25
10
votes
1 answer
list of objects python
I am trying to print a list of python objects that contain a list as a property and i am having some unexpected results:
here is my code:
class video(object):
name = ''
url = ''
class topic(object):
topicName = ''
listOfVideo =…

user254340
- 447
- 2
- 7
- 21
7
votes
3 answers
how to get the attribute of setter method of property in python
Please consider the below code
class DataMember():
def __init__(self, **args):
self.default = {"required" : False , "type" : "string" , "length": -1}
self.default.update(args)
def __call__(self , func):
#Here I want to set the…

Anubrij Chandra
- 1,592
- 2
- 14
- 22
5
votes
3 answers
Python multiprocessing making same object instance for every process
I have written a simple example to illustrate what exactly I'm banging my head onto. Probably there is some very simple explanaition that I just miss.
import time
import multiprocessing as mp
import os
class SomeOtherClass:
def…

Mario Kirov
- 341
- 1
- 11
3
votes
1 answer
Loading same-named classes with importlib based on directory name: What are the consequences?
Of course "no consequences except the obvious" is a valid answer to my question about the consequences.
I help maintain a code base, with some proposed code structured roughly like this:
# main.sh
export PYTHONPATH=$PYTHONPATH:$(pwd)
python main.py…

Philip
- 323
- 3
- 13
3
votes
2 answers
Is "everything" in Python (3) an instance of some class?
I am new to Python but have already read a number of times the principle that everything in Python is an object.
Does that mean that everything is an instance of some class?
As an example, suppose we have a function f. Please then consider running…

Cabbage
- 151
- 4
3
votes
2 answers
How to validate a class attribute in python?
I have been trying to validate classes that users can create in a framework style setting.
I can ensure that a class attribute is present in child classes in the following manner:
from abc import ABC, abstractmethod
class A(ABC):
@property
…

Guilherme Marthe
- 1,104
- 9
- 18
2
votes
2 answers
type(x) is list vs type(x) == list
In Python, suppose one wants to test whether the variable x is a reference to a list object. Is there a difference between if type(x) is list: and if type(x) == list:? This is how I understand it. (Please correct me if I am wrong)
type(x) is list…

lamc
- 347
- 2
- 5
2
votes
2 answers
Is there a way to disable some function in python class so that it cannot be used except using it in inside its class?
for example i have myClassFile.py file with code as follow:
class myClass:
def first(self):
return 'tea'
def second(self):
print(f'drink {self.first()}')
then i have run.py file with code as follow:
from myClassFile import…

Hanzcerb
- 51
- 6
2
votes
1 answer
Why does setting a method in a python class does not pass self when called?
If I write this:
class A:
def a(self):
return 2
def b(self):
print(self.a())
e = A()
def xa(self):
return 3
e.a = xa
e.b()
will explode saying that:
TypeError: xa() missing 1 required positional argument: 'self'
why…

gotch4
- 13,093
- 29
- 107
- 170
2
votes
2 answers
How to extend Symbol class in sympy?
I’m having trouble extending the Symbol class in sympy. It could be a result of something with class extensions in general, or it might also be an issue with this specific “Symbol” class.
I want to extend the Symbol class to have an additional…

makansij
- 9,303
- 37
- 105
- 183
2
votes
3 answers
Python: automatically call a parent function after child instantiation
Python 2.7
I would like to automotically call a function of a parent object after I instantiate its child
class Mother:
def __init__(self):
pass
def call_me_maybe(self):
print 'hello son'
class Child(Mother):
def…

ThriceGood
- 1,633
- 3
- 25
- 43
1
vote
4 answers
how to create a python object where every attribute is None
I'm not sure if this can be done, but I would like an object where you can query any attribute, and it just returns None, instead of an error.
eg.
>>> print(MyObject.whatever)
None
>>> print(MyObject.abc)
None
>>>…

aloea
- 191
- 1
- 12