instance

What is the need to define a function to return a class's attribute?

What is the need to define a function to return a class's attribute? Question: I came across some code like this class Student(Person): def __init__(self, age): self.age = age def get_age(self): return self.age Could you please explain the purpose of the function get_age()? If we want to know a student’s age, can’t we simply do …

Total answers: 1

How to create conditional instance attribute in python which can only take value >= 5000,else prompt user that minimum is 5000?

How to create conditional instance attribute in python which can only take value >= 5000,else prompt user that minimum is 5000? Question: I have class ‘Player’ in python and wanna create instance attribute ‘deposit’,which intended take integer equal or greather 5000,else raise error or to ask user set valid value.So I tried implement that with …

Total answers: 1

How to assign a list of instance methods to a class variable?

How to assign a list of instance methods to a class variable? Question: I have this class which works: class Point: def __init__(self): self.checks = [self.check1, self.check2] def check1(self): return True def check2(self): return True def run_all_checks(self): for check in self.checks: check() The instance variable checks is not specific to instances, so I want to …

Total answers: 1

Check whether a variable is instance of ResNet50

Check whether a variable is instance of ResNet50 Question: I am checking whether model = ResNet50(weights=’imagenet’, include_top=False, pooling="avg") is instance of keras.applications.ResNet50 What I have done is: isinstance(model, ResNet50) but unfortunately this is raising me the following exception: TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union Moreover, I …

Total answers: 1

Python use instance of class in a function in another file

Python use instance of class in a function in another file Question: I have three files, app.py, server.py and tools.py the files are quite big in total, so i’ll just show excerpts server.py is basically just flask app.py looks about like this from server import Server from tools import Tools if __name__ == "__main__": web …

Total answers: 2

How to intercept the instantiated class name from the function?

How to intercept the instantiated class name from the function? Question: I encountered an issue in one of my projects, I managed to reduce it to a simplest example possible. Consider the following class A: def f(self): return ‘I am f()’ class B(A): def g(self): return ‘I am g()’ a = A() b = B() …

Total answers: 2

saving class instances with json file – Python

saving class instances with json file – Python Question: This is the class I am currently using class Book: def __init__(self, title: str, author: str, isbn: int, genre: str, numCopies: int): self._title = title self._author = author self._isbn = isbn self._genre = genre self._numCopies = numCopies def __str__(self): # all the properties of the class …

Total answers: 1

How to retrieve an attribute from a list of instances properly

How to retrieve an attribute from a list of instances properly Question: I am unsure how to correctly get the attribute from a list of instances. Sorry if this seems simple, I am only a beginner. class Clown: def __init__(self,name,hours,job): self.name = name self.hours = hours self.job = job def get_job(self): return self.job def change_job(self, …

Total answers: 1

confusion because i can't figure out what is changing the attribute of the object of a class in python

confusion because i can't figure out what is changing the attribute of the object of a class in python Question: Here is my code ` class Cats: def __init__(self,name): self.name=name #print(self.name,name,"hello") def change_name(self,new_name): self.name=new_name return 0 #print(new_name) cat1=Cats("lion") print(cat1) print(cat1.name) cat2=cat1.change_name("tiger") print(cat1.name) print(cat1) print(cat2) ** Here is the output with my comments/opinion on the side …

Total answers: 1