class

Multidimensional list of classes - overwriting issue

耗尽温柔 提交于 2021-01-29 05:18:26
问题 I want to create a list of classes, but every time I change an element in the list, all class elements in the list are overwritten: class TypeTest: def __init__(self): self.val = int() data = list([TypeTest for _ in range(3)]) for i in range(3): data[i].val = i print([data[0].val, data[1].val, data[2].val]) At the end, I need a multidimensional array and will have a more complex class, but the issue is the same. 回答1: TypeTest is a type; TypeTest() creates an instance of that type. You need

Initialising nested classes in Python

匆匆过客 提交于 2021-01-29 05:17:37
问题 Let's say I want to create a class 'House' that has some attributes of its own, but also has a (nested?) 'Resident' class which has some attributes and has a mandatory attribute 'surname'. A house instance may exist though without any residents. How can create this so that I can eventually do the following? myhouse = House() residentX = myhouse.resident('Smith') Currently I set this up as a nested class but run into trouble when I try and initialise myhouse given that it is requiring a

How to avoid globals (and use classes)?

旧时模样 提交于 2021-01-29 05:02:30
问题 To have some exercise in learning Python, and especially with Object orientated programming. I'm creating a simple text based game. I'm a bit struggling with the use of global variables. People say it's better to avoid them. My question how can I make thing work without them and where to declare those variables. Currently in my main() method I'll start the game based on Classes for every room or interaction that can happen in the game. But there are some objects I want to access anytime, like

Invoke in Class in c# winforms

ぐ巨炮叔叔 提交于 2021-01-29 04:41:44
问题 I have a thread running in a class that needs to update value of textbox but invoke does not appear in class. Any idea how to do it? using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Text.RegularExpressions; using System.Windows.Forms; using System.IO; using CheckedBoxSerpCrawler; namespace SERP_Crawler { class Crawl { public Crawl() { var t = new Thread(() => { for (int i = 2; i < (pagesToScroll / 10); i++) { //Here i

how to declare member variable as extended type in TypeScript?

和自甴很熟 提交于 2021-01-29 04:13:04
问题 Is there a way to define a "member variable" as an "extension object" instead of a static type (without using an interface)? Simply something like this pseudocode: class Foo { bar -> extends Rectangle; constructor(barInstance:IRectangle){ this.bar = barInstance; this.bar.getArea(); //<-- is code completed because interface IRectangle // no type error this.bar.someCustomFunction = function() { } } } instead of class Foo { bar: IRectangle; //or bar:Rectangle; } This way I can add properties not

scipy rv_continuous very slow

我只是一个虾纸丫 提交于 2021-01-29 04:01:27
问题 I am using a custom function f(x) to define a custom distribution using copy 's rv_continuous class. My code is class my_pdf_gen(rv_continuous): def _pdf(self, x, integral): return f(x)/integral where integral ensure the normalisation. I am able to create an instance of it with my_pdf = my_pdf_gen(my_int,a = a, b = b, name = 'my pdf') with a,b the upper and lower limit of the value's range, and my_int= scipy.integrate.quad(f, a, b)[0] . I am also able to create a random sample of data using

How do I use a method outside a class?

夙愿已清 提交于 2021-01-29 03:20:38
问题 I'm taking python classes. I've asked for hints about this in our forums but with no luck. I think my implementation is very bad. I'm very new at this, so bear with me, even with the way I ask the question. The question above is what I am told I need to do. I've tried but with no luck, so I've come here for help. Ultimately, I am trying to get my key handlers to respond to my keypresses. I've done this previously, but we were not yet working with classes. That's where the snag is. I'm

Understanding data encapsulation in Python

不打扰是莪最后的温柔 提交于 2021-01-29 02:51:57
问题 I am reading up on how we ensure data encapsulation in python.One of the blog says "Data Encapsulation means, that we should only be able to access private attributes via getters and setters" Consider the following snippets from the blog: class Robot: def __init__(self, name=None, build_year=None): self.name = name self.build_year = build_year Now, if i create the object of the class as below: obj1=Robot() obj1.name('Robo1") obj1.build_year("1978") Currently, i can access the attributes

Understanding data encapsulation in Python

跟風遠走 提交于 2021-01-29 02:51:47
问题 I am reading up on how we ensure data encapsulation in python.One of the blog says "Data Encapsulation means, that we should only be able to access private attributes via getters and setters" Consider the following snippets from the blog: class Robot: def __init__(self, name=None, build_year=None): self.name = name self.build_year = build_year Now, if i create the object of the class as below: obj1=Robot() obj1.name('Robo1") obj1.build_year("1978") Currently, i can access the attributes

C++ can't use class from another file

倖福魔咒の 提交于 2021-01-28 23:08:19
问题 I'm C++ by writing small programs. I'm too the point of working with multiple files. I'm stuck on using a class from another file. I made a simple test project to demonstrate my problem. I have 3 files. testheader.h #ifndef __testheader_H_INCLUDED__ // if Node.h hasn't been included yet... #define __testheader_H_INCLUDED__ // #define this so the compiler knows it has been included #include <string> #include <iostream> class testheader { public: testheader(std::string name){} void write(){} };