Category : oop

I have this example: class MyList(list): some_attr = 1 def some_op(x): return x def func_1(xs): if not isinstance(xs, list): return xs return xs.__class__([some_op(x) for x in xs]) def func_2(xs): return xs.__class__([some_op(x) for x in xs]) items = MyList([1, 2, 3]) items_1 = func_1(items) print(type(items_1), items_1, hasattr(items_1, ‘some_attr’)) items_2 = func_2(items) print(type(items_2), items_2, hasattr(items_2, ‘some_attr’)) Which ..

Read more

There is an object trade_pair whose methods are able to return float values about stored in quotes and values (the number of both currencies, the current price, etc.). Only mathematical float values that are suitable for calculations, but are not suitable for a human and sending to the exchange API (there are requirements for the ..

Read more

I am learning Python and having a hard time wrapping my head about some of the concepts of OOP, classes, and properties I created the following code: class BankAccount: def __init__(self, name, balance): self._name = name #Encapsulate self._balance = balance #Encapsulate self._transaction_fee = 5.00 #Encapsulate def deposit(self, amount): if (self._balance + amount) < 0: raise ..

Read more

I have a class which is used to hold a bunch of python static methods which will return a string from an environment variable. class GitLabCiEnv(): """ Gitlab CI Environment variables. """ @staticmethod def CHAT_CHANNEL() -> str: """ Source chat channel which triggered the ChatOps command. Added in GitLab 10.6 Available in GitLab Runner all ..

Read more

I have this test program: class BankAccount: def __init__(self, name, balance): self.name = name self.balance = balance self.transaction_fee = 5.00 def deposit(self, amount): self.balance = self.balance + amount def withdraw(self, amount): self.balance = self.balance – amount I encapsulated the balance, name and transaction fee by doing the below: class BankAccount: def __init__(self, name, balance): self._name ..

Read more

In django/contrib/admin/sites.py, I have class AdminSite: def get_app_list(self): //do something pass def an_other_to_change(self): //do something pass def not_to_change(self): //do something pass I want to change/override implementation of few methods of this class so that default function should have some thing new in it what i want. To be sure that my code is working I ..

Read more