Questions tagged [python-object]

102 questions
1
vote
2 answers

Pythonic way to get either one of two attributes from a Python object

I want to get either one of two attributes from a Python object. If neither of those attributes is present, I want to get a default value. For instance, I want to get either the number of apples or of oranges in a FruitBasket, and if neither is…
Enrico Gandini
  • 855
  • 5
  • 29
1
vote
0 answers

Assigning starting player and switching between them

I am trying to assign a starting player as the "first" CurrentPlayer, and then assign player 2 as CurrentPlayer once player 1 has finished the turn. Somehow, I cant get it to work at all. """ This is the model of the game""" class Game: …
1
vote
1 answer

How to find out what `*` operator do on two objects from some library (cirq)?

I just started using python cirq library and I came across the following line of code in the tutorial: XX_obs = cirq.X(q0) * cirq.X(q1) I just want to find in the code what this * operator do on this two specific cirq objects. How to do so?
Fallen Apart
  • 723
  • 8
  • 20
1
vote
2 answers

How is this Python design pattern called?

In the ffmpeg-python docs, they used following design pattern for their examples: ( ffmpeg .input('dummy.mp4') .filter('fps', fps=25, round='up') .output('dummy2.mp4') .run() ) How is this design pattern called, where can I find…
TheEagle
  • 5,808
  • 3
  • 11
  • 39
1
vote
0 answers

DJANGO python error return qs._result_cache[0]

Im new using Django, Im coding for an ecommerce and everything is working as expected but an error raised even when the code is working! Sorry for spanglish: id = request.POST.get('id') tamano = request.POST.get('size') items =…
1
vote
0 answers

Override a sloted attribute with a class variable in python

The code below: class A(object): __slots__ = 'a' def __init__(self, a): self.a = a print (A(4).a) # 4 class B(A): a = 4 __slots__ = 'b' B(4) blows with: Traceback (most recent call last): File "", line 1, in…
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
1
vote
1 answer

I want to call parent class method which is overridden in child class through child class object in Python

class abc(): def xyz(self): print("Class abc") class foo(abc): def xyz(self): print("class foo") x = foo() I want to call xyz() of the parent class, something like; x.super().xyz()
Syed Fasih
  • 23
  • 6
1
vote
1 answer

Question about multiple inheritance, dynamic class creating and instantiation

Hello I have the following situation: A specialized class that inherits from two parent class The need to define the most specialized class at run time, based on some information that I get only when I start reading data from a database. I…
Cracoras
  • 347
  • 3
  • 16
1
vote
1 answer

Returning StringIO object

I have the following python code: def parse_object(object): data = object.read() do_other_stuff(data) def get_object(): content = "abc" try: object = StringIO() object.write(content) return object …
Bojangles
  • 39
  • 4
1
vote
1 answer

Python: How can I initialise object in one module and use it in another

my setup: mod1.py: class cars: def __init__(self,x,y,z): self.x = x mod2.py: import mod1 obj = mod1.cars(x,y,z) mod3.py from mod2 import obj Now, what's happening is when I am importing obj in mod3.py init method of cars is getting…
TheBeginner
  • 405
  • 5
  • 23
1
vote
4 answers

Getting inputs to a function from a python object

I have a class. This class has a list of functions that are to be evaluated by a different program. class SomeClass(object): def __init__(self, context): self.functions_to_evaluate = [] There is a function that adds functions to an…
RebeccaK375
  • 871
  • 3
  • 17
  • 28
1
vote
3 answers

Positional arguments and the self keyword

class Employee: def __init__(self, first, last): self.first = first self.last = last def fullname(): return '{} {}'.format(self.first, self.last) emp = Employee('Rob', 'M') print (emp.fullname()) You will notice…
Rob
  • 545
  • 3
  • 21
1
vote
1 answer

Python @property setter not setting from instance call

I have a class (simplified view): class Record(): OrderStatusMapping = dict(zip(['0', '1', '2'], ['NEWO', 'PARF', 'FILL'])) def __init__(self): self._OrderStatus = None @property def OrderStatus(self): return…
Jakob
  • 1,129
  • 9
  • 24
1
vote
1 answer

How can we get a definitive list of everything (or nearly everything) stored in a python object?

When I say "stored" I mainly mean attributes, not container elements or values returned by __getitem__. My first thought was to bypass any overloaded __getattribute__ method (aka, dot-operator) with object.__getattribute__. I thought I would then…
Toothpick Anemone
  • 4,290
  • 2
  • 20
  • 42
1
vote
1 answer

How to sum attributes of all instances of an object

I want to sum the costsum attribute for all the instances of an object. class ActivityCenter: def __init__(self, costpool, costsum, costdriver, cdunits): self.costpool = costpool self.costsum = costsum self.costdriver =…