Pythonistas! First of all, I am a beginner. I just started reading and practicing the Python Crash Course book. I have reached the OOP chapter where I was given an assignment. The problem is I am getting a NameError which makes no sense as I have already defined the name. My code is: class Restaurants(): ..
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 ..
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 ..
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 ..
I faced a task what sound like: "write 2 functions (func, func1) that will come to result x with code <func(x); func1()>". Right now I can’t come to the right thought, please help. Source: Python-3x..
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 ..
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 ..
What I am trying to achieve: So, I am trying to use OOP to create a GUI with tkinter in which I can create Windows that have a standard UI. Haven’t done this before so I am just messing around at the moment to familiarize myself with tkinter, and OOP at that. In saying this, ..
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 ..
So I am trying to build a simple chess program that can be used to display and move chess pieces. My Idea was to create a class for the board that basically combines the tile-class/tile-objects and hopefully soon the piece-objects. In the init function of the board class I use two lists "ranks" and "files" ..
Recent Comments