class

Python make a circular list to get out of range index

放肆的年华 提交于 2021-02-05 11:29:10
问题 I'm looking for ways to make a list into circular list so that I can call a number with index out of range. For example, I currently have this class: class myClass(list): definitely __init__(self, *x): super().__init__(x) which works as: >> myList = myClass(1,2,3,4,5,6,7,8,9,10) >> print(myList) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and after creating a list, I want to: >> myList[2] 3 >> myList[12] 3 >> myList[302] 3 >> myList[-1] 10 >> myList[-21] 10 ... At the moment, index [12], [302] and [-21]

How to create a custom string representation for a class object?

馋奶兔 提交于 2021-02-05 11:24:26
问题 Consider this class: class foo(object): pass The default string representation looks something like this: >>> str(foo) "<class '__main__.foo'>" How can I make this display a custom string? 回答1: Implement __str__() or __repr__() in the class's metaclass. class MC(type): def __repr__(self): return 'Wahaha!' class C(object): __metaclass__ = MC print C Use __str__ if you mean a readable stringification, use __repr__ for unambiguous representations. 回答2: class foo(object): def __str__(self):

How to set the Return Type of a Class Member Function as the object of a Private Struct

孤者浪人 提交于 2021-02-05 09:42:41
问题 sorry for the long and confusing title, but I couldn't think of a better way to ask this. So, what I have is a class: template <typename T> class Set { public: //random member functions here private: struct Node{ T key; Node *right; Node *left; int height; }; public: Node* r_add(Node *temp); }; Node* Set<T>::r_add(Node *temp) { return temp; } When I try to implement the function r_add, I keep getting the error that the return type of out-of-line definition differs from that in the declaration

Using map with class error, compile error

有些话、适合烂在心里 提交于 2021-02-05 09:41:38
问题 I have the following compiler error, how could I fix it? error: instantiated from `_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = ar, _Tp = int, _Compare = std::less<ar>, _Alloc = std::allocator<std::pair<const ar, int> >]' This is the code: #include <map> #include <cstdio> #include <iostream> #include <algorithm> #include <cstdlib> using namespace std; class ar { public: int a; int b; int c; public: ar() : a(0), b(0), c(0) {} }; int main() { map<ar, int>

Python chainable class methods

孤者浪人 提交于 2021-02-05 09:39:19
问题 I want to do the following: pattern = cl().a().b("test").c() where cl is a class and a, b, c are class methods. After that I need to call pattern.to_string and it should output a string that was formed. Each method returns a string. Now how can I achieve the above? Append the method output to a list? What about the chainable function? If I wrote the class the normal way, the above won't work. Thank. 回答1: Return the class instance at the end of each method and store the intermediate results in

python/pygame, pass input from a class to another class

只愿长相守 提交于 2021-02-05 09:33:08
问题 there is a way to pass a value or a variable from a class to another class without having to pass through the main function I'm using python 回答1: well, of course you can access other objects attributes in methods of a specific object. e.g: class A(object): def method(self, other): other.somevar = 5 class B(object): pass def main(): a = A() b = B() b.somevar = "Hello World" a.method(b) print(b.somevar) # now prints '5' 来源: https://stackoverflow.com/questions/7610531/python-pygame-pass-input

python/pygame, pass input from a class to another class

你。 提交于 2021-02-05 09:31:02
问题 there is a way to pass a value or a variable from a class to another class without having to pass through the main function I'm using python 回答1: well, of course you can access other objects attributes in methods of a specific object. e.g: class A(object): def method(self, other): other.somevar = 5 class B(object): pass def main(): a = A() b = B() b.somevar = "Hello World" a.method(b) print(b.somevar) # now prints '5' 来源: https://stackoverflow.com/questions/7610531/python-pygame-pass-input

C++ error, Undefined reference class

允我心安 提交于 2021-02-05 09:25:28
问题 Why does codeblocks give this error "Undefined reference to class::classfunction()" It happens when a class is created in a separated file.All of these files are in the same folder This is the main .cpp file #include<iostream> #include "Class2.h" using namespace std; main() { Class2 classObject; cout<<"I'm class2"<<endl; } class header file #ifndef CLASS2_H #define CLASS2_H class Class2 { public: Class2(); ~Class2(); protected: private: }; #endif // CLASS2_H class cpp file #include "Class2.h"

Need help an error “TypeError: __init__() takes 5 positional arguments but 6 were given”

情到浓时终转凉″ 提交于 2021-02-05 09:13:47
问题 I am just started getting into classes and understand the basic concept however I don't understand why it gives me this error. My code: import pygame pygame.init() screen = pygame.display.set_mode((400, 300)) done = False while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True class shape(): def __init__(self, place, colour, x, y): self.place = place self.colour = colour self.x = x self.y = y class rectangle(shape): def __init__(self, place, colour, x, y,

Need help an error “TypeError: __init__() takes 5 positional arguments but 6 were given”

随声附和 提交于 2021-02-05 09:13:25
问题 I am just started getting into classes and understand the basic concept however I don't understand why it gives me this error. My code: import pygame pygame.init() screen = pygame.display.set_mode((400, 300)) done = False while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True class shape(): def __init__(self, place, colour, x, y): self.place = place self.colour = colour self.x = x self.y = y class rectangle(shape): def __init__(self, place, colour, x, y,