Category : class

I want to be able to reshuffle/re-randomize the numbers which is outputs. Is there any way to do that from anywhere in the code? Here’s the class which I’m referencing: class Foe(Actor): #v constructor v def __init__(self, image): super().__init__(image) self.rotation = random.uniform(-2.5, 2.5) firside = random.randint(1, 8) #picks a random number between 1 and 8 ..

Read more

I have a fraction class that looks like this: class Fraction: def __init__(self,top,bottom): self.num = top self.den = bottom def __str__(self): return str(self.num)+"/"+str(self.den) def show(self): print(self.num,"/",self.den) def __add__(self,otherfraction): newnum = self.num*otherfraction.den + self.den*otherfraction.num newden = self.den * otherfraction.den common = gcd(newnum,newden) return Fraction(newnum//common,newden//common) def __eq__(self, other): firstnum = self.num * other.den secondnum = other.num * ..

Read more

class BankAccount(): total_accounts = 0 def __init__(self, savings_accounts, checking_accounts): self.savings_accounts = savings_accounts self.checking_accounts = checking_accounts def displayCounter1(self): print(total_accounts) class Savings_Accounts(BankAccount): savingsBalance = 5000000.00 def __init__(self, savings_accounts, checking_accounts): self.savings_accounts = savings_accounts self.checking_accounts = checking_Accounts class Checking_Accounts(BankAccount): checkingsalance = 50000000.00 def __init__(self, savings_accounts, checking_accounts): self.savings_accounts = savings_accounts self.checking_accounts = checking_Accounts def displayCounter(self): print(checkingsalance) n = Checking_Accounts(BankAccount) n.displayCounter() ..

Read more

I have 2 models Model A Fields : a_id, a_name, common_name and Model B Fields : b_id, b_name, common_name and my Serializer is like below for Model B class ModelBSerializer: model_a_obj_list = model_a.objects.all().values(a_id a_name common_name) common_name_list = {} for model_a_obj in model_a_obj_list: common_name_list[model_a_obj[‘common_name’]] = { a_name: model_a_obj[‘a_name’] a_id: model_a_obj[‘a_id’] } a_model_details = fields.SerializerMethodField(read_only=True) def get_a_model_details(self, ..

Read more

I have a dataclass of a single class object ‘AStock’ as such: @dataclass class AStock: code_: Union[str, int] force_update_: bool = False ticker_: str = field(init=False) symbol_: str = field(init=False) name_: str = field(init=False) sector_: str = field(init=False) subsector_: str = field(init=False) @exception(logger) def __post_init__(self) -> None: try: self.code_ = str(self.code_).zfill(4) self.ticker_ = indexer(code=self.code_, arg=’ticker’) ..

Read more

I’m writing a set of custom classes with inheritance using Python. Here is the code, simplified: class Parent(object): __slots__ = [‘a’,’b’] def __init__(self, a:int, b:int): self.a = a self.b = b def myfunc(self,s)->int: return self.a + s.a class Child(Parent): __slots__ = [‘c’] def __init__(self, a:int, b:int, c:int): super().__init__(a,b) self.c = c def myfunc(self,s)->int: return self.a ..

Read more