Questions tagged [class-attributes]

170 questions
-2
votes
2 answers

Python - Unexpected behaviour of class attributes

Edited in simple words code: class temp: attr1 = 0 attr2 = [] t1 = temp() t2 = temp() t1.attr1 = 50 t1.attr2.append(50) print(t1.attr1) print(t1.attr2) print(t2.attr1) print(t2.attr2) output: 50 [50] 0 [50] I have called append only on…
Ani
  • 2,848
  • 2
  • 24
  • 34
-2
votes
1 answer

Print all class attributes (not instance attributes)

I want to print all class attributes. Not instance attributes. So I want to get ['list1','list2'] output. class MyClass(): list1 = [1,2,3] list2 = ['a','b','c'] def __init__(self, size, speed): self.size = size …
-2
votes
1 answer

Update an attribute each time the class constructor is invoked

I have this class in Python. (Removed the unecessary parts) class Spillebrett: def __init__(self, rader, kolonner): self._rader = rader self._kolonner = kolonner self._generasjonsnummer = 0 I need to add 1 to…
-3
votes
1 answer

How to print one Attribute in a class in Python?

simply i have a class that has a method to print a specific attibute in a class example: class Attr: def __init__(self, name, wattage): self.name = name self.wattage = wattage def print_attr(self): …
Newbieee
  • 167
  • 10
-4
votes
2 answers

Changing the value of a class attribute using an instance

I am learning about classes and objects in python. I encountered a problem when I tried to create a class attribute whose value can be changed using an instance of that class. Lets assume create a class Student for students who go to the same…
Amani
  • 57
  • 4
1 2 3
11
12