instance

How to create child class instance using some magic in parent __new__?

柔情痞子 提交于 2019-11-30 09:52:37
问题 For example, creating custom number types in following hierarchy Number Complex Real Int Float with logic in __new__ methods: class Number: def __new__(cls, value): if isinstance(value, complex): return Complex(value) elif isinstance(value, (int, float)): return Real(value) else: raise TypeError('Ну ты и мудак!!!') def __init__(self, value): self.value = value class Complex(Number): pass class Real(Number): def __new__(cls, value): if isinstance(value, int): return Int(value) elif isinstance

How to instantiate an object in TypeScript by specifying each property and its value?

℡╲_俬逩灬. 提交于 2019-11-30 09:05:29
Here's a snippet in which I instantiate a new content object in my service: const newContent = new Content( result.obj.name result.obj.user.firstName, result.obj._id, result.obj.user._id, ); The problem is that this way of object instantiation relies on the order of properties in my content model. I was wondering if there's a way to do it by mapping every property to the value I want to set it to, for example: const newContent = new Content( name: result.obj.name, user: result.obj.user. content_id: result.obj._id, user_id: result.obj.user._id, ); const newContent = <Content>({ name: result.obj

Accessing an instance of a variable from another class in Java

扶醉桌前 提交于 2019-11-30 06:05:04
问题 Is it possible to access an instance of a variable in one class from another class in Java. Let's say you have the following in Class A: private BlockingQueue<byte[]> buffer = new LinkedBlockingQueue<byte[]>(); I want to make changes to the queue in this class and then be able to use to access it from another class. How would i access the instance of buffer from another class? Is it even possible? 回答1: Add a getter: public class Whatever { private BlockingQueue<byte[]> buffer = new

Groovy Mixin on Instance (Dynamic Mixin)

若如初见. 提交于 2019-11-30 05:20:01
问题 I'm trying to achieve following: class A { def foo() { "foo" } } class B { def bar() { "bar" } } A.mixin B def a = new A() a.foo() + a.bar() with one significant difference - I would like to do the mixin on the instance: a.mixin B but this results in groovy.lang.MissingMethodException: No signature of method: A.mixin() is applicable for argument types: (java.lang.Class) values: [class B] Is there a way to get this working like proposed in the Groovy Mixins JSR? 回答1: You can do this since

Convert String to Object name

蓝咒 提交于 2019-11-30 05:15:11
问题 I needed help on this one. How do I convert a string into a variable/object/instance name, since I don't know how to categorize this. Assuming my code is: a = {} b = {} class Test: def getKeys(self, var): return var.keys() #where var refers to the dictionary and its a string initially 回答1: It's been forever since I've done anything with Python but I do remember having this problem once. I suggest you look into the eval() function. 回答2: If I understand correctly, your variable var is a string

Create an anonymous class instance in python

蓝咒 提交于 2019-11-30 04:43:20
Sometimes i need to create an anonymous class instance in python, just like c#: var o= new {attr1="somehing", attr2=344}; but in python i do it in this way: class Dummy: pass o = Dummy() o.attr1 = 'something' o.attr2 = 344 #EDIT 1 print o.attr1, o.attr2 how can do that in pythonic way in single statement? o = type('Dummy', (object,), { "attr1": "somehing", "attr2": 344 }) o.attr3 = "test" print o.attr1, o.attr2, o.attr3 type while this is not precisely a single statement I think creating a wrapper around the magic of the accepted answer makes it by far more readable. import inspect # wrap the

JavaScript: How to create a new instance of a class without using the new keyword?

为君一笑 提交于 2019-11-30 03:10:24
I think the following code will make the question clear. // My class var Class = function() { console.log("Constructor"); }; Class.prototype = { method: function() { console.log("Method");} } // Creating an instance with new var object1 = new Class(); object1.method(); console.log("New returned", object1); // How to write a factory which can't use the new keyword? function factory(clazz) { // Assume this function can't see "Class", but only sees its parameter "clazz". return clazz.call(); // Calls the constructor, but no new object is created return clazz.new(); // Doesn't work because there

best way to create object

元气小坏坏 提交于 2019-11-30 01:37:20
This seems to be very stupid and rudimentary question, but i tried to google it, but couldn't a find a satisfactory answer, public class Person { public string Name { get; set; } public int Age { get; set; } public Person(){} public Person(string name, int age) { Name = name; Age = age; } //Other properties, methods, events... } My question is if i have class like this, what is the best way to create an object? Person p=new Person("abc",15) OR Person p=new Person(); p.Name="abc"; p.Age=15; What is the difference between these two methods and what is the best way to create objects? davioooh

Static classes in Python

半城伤御伤魂 提交于 2019-11-30 00:17:56
问题 I once read (I think on a page from Microsoft) that it's a good way to use static classes, when you don't NEED two or more instances of a class. I'm writing a program in Python. Is it a bad style, if I use @classmethod for every method of a class? 回答1: In my experience creating a class is a very good solution for a number of reasons. One is that you wind up using the class as a 'normal' class (esp. making more than just one instance) more often than you might think. It's also a reasonable

java singleton instantiation

巧了我就是萌 提交于 2019-11-29 22:40:56
I've found three ways of instantiating a Singleton, but I have doubts as to whether any of them is the best there is. I'm using them in a multi-threaded environment and prefer lazy instantiation. Sample 1: private static final ClassName INSTANCE = new ClassName(); public static ClassName getInstance() { return INSTANCE; } Sample 2: private static class SingletonHolder { public static final ClassName INSTANCE = new ClassName(); } public static ClassName getInstance() { return SingletonHolder.INSTANCE; } Sample 3: private static ClassName INSTANCE; public static synchronized ClassName