instance

Having problems showing an attribute from instance class

Having problems showing an attribute from instance class Question: I know there’s a similar post but following it I couldn’t solve the problem. I have a main class ‘User’, a subclass ‘Admin’ that inherits the methods from ‘User’. Then I have a class ‘Priviledges’. Inside ‘Admin’ there’s a ‘Priviledges’ instance. I want ‘Priviledges’ to show …

Total answers: 1

Can someone explain the working of the statement in the provided piece of code?

Can someone explain the working of the statement in the provided piece of code? Question: I have just started OOPs in python and came across this piece of code . I was trying to understand the code in https://pythontutor.com/render.html#mode=display but could not understand the output of first if statement , if float value is passed(like …

Total answers: 2

Calling an instance method without creating an instance of class in Python

Calling an instance method without creating an instance of class in Python Question: I read on that instance methods can only be called by creating an instance (object) of the class. But it appears that I can call one without doing so. Check the code below: class Test: def func(self): #Instance Method print(6) Test.func(Test) # …

Total answers: 1

Python: Sort, randomize specific attributes, then manipulate

Python: Sort, randomize specific attributes, then manipulate Question: I need to find the lowest attribute value, add 1, as well as randomly manipulate a random attribute value for a given set of class instance attributes. My ‘Player’ class has many attributes, self.name = name self.level = 1 self.experience = 0 self.gold = 500000 self.wielded_weapon = …

Total answers: 1

How can I make a class object callable in Python?

How can I make a class object callable in Python? Question: How can you define in Python3 a class MyClass such that you instantiate it like obj = MyClass(param1, param2) and then use it to compute an operation like res = obj(in1, in2, in3) ? For instance, with PyTorch you can declare a model as …

Total answers: 2

Why isn't my class variable changed for all instances in Python?

Why isn't my class variable changed for all instances in Python? Question: I’m learning about classes and don’t understand this: class MyClass: var = 1 one = MyClass() two = MyClass() print(one.var, two.var) # out: 1 1 one.var = 2 print(one.var, two.var) # out: 2 1 I thought that class variables are accessible by all …

Total answers: 3

Python tkinter: Open only one instance at a time of secodary window from primary window

Python tkinter: Open only one instance at a time of secodary window from primary window Question: There are two windows PRIMARY and SECONDARY, I want to open secondary window using a button widget on primary window. But problem is whenever I press that button secondary window is opened, doesn’t matter if a secondary window is …

Total answers: 3

How to delete property?

How to delete property? Question: class C(): @property def x(self): return 0 delattr(C(), ‘x’) >>> AttributeError: can’t delete attribute I’m aware del C.x works, but this deletes the class‘s property; can a class instance‘s property be deleted? Asked By: OverLordGoldDragon || Source Answers: I’m aware del C.x works, but this deletes the class’s property; can …

Total answers: 4