instance

Why is instance variable behaving like a class variable in Python? [duplicate]

主宰稳场 提交于 2019-11-29 15:02:20
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument I have the following code: class Node(object): def __init__(self, value = 0, children = {}): self.val = value self.children = children def setChildValue(self, index, childValue): self.children[index] = Node(childValue) n = Node() n.setChildValue(0,10) print n.children n2 = Node() print n2.children And it prints: {0: <__main__.Node object at 0x10586de90>} {0: <__main__.Node object at 0x10586de90>} So my question is, why is children defined in n2? Children is an instance variable and yet it's acting like a class

Python creating multiple instances for a single object/class

浪尽此生 提交于 2019-11-29 14:43:29
问题 I'm using Python. I've read a bit about this and can't seem to wrap my mind around it. What I want to do is have a class called Potions with various potion objects in it. For now there's one potion, a simple HealthPotion. I want potions to be stackable in inventories and shop stocks. So I need an instance of the potion amount for an inventory and an instance for each shop stock that carries potions. The potion amount would be dynamic, for buying/selling and looting of potions. If someone

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

六眼飞鱼酱① 提交于 2019-11-29 13:52:27
问题 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.

@instance_variable not available inside a ruby block?

家住魔仙堡 提交于 2019-11-29 12:16:54
问题 With the following code: def index @q = "" @q = params[:search][:q] if params[:search] q = @q @search = Sunspot.search(User) do keywords q end @users = @search.results end If @q is used instead of q, the search always returns results for an empty query (""). Why is this? Is the @q variable unavailable to the do...end block? 回答1: It depends on how the block is being called. If it is called using the yield keyword or the Proc#call method, then you'll be able to use your instance variables in

How to iterate over multiple Word instances (with AccessibleObjectFromWindow)

半腔热情 提交于 2019-11-29 12:03:09
I need to iterate over all Word instances, no matter if opened by users, by automation, zumbis, etc. I will describe all the steps until now: I saw and implemented the solutions that I got here ; Do For Each objWordDocument In objWordApplication.Documents OpenDocs(iContadorDocs - 1) = objWordDocument.Name OpenDocs(iContadorDocs) = objWordDocument.path iContadorDocs = iContadorDocs + 2 ReDim Preserve OpenDocs(iContadorDocs) Next objWordDocument iWordInstances = iWordInstances + 1 objWordApplication.Quit False Set objWordApplication = Nothing Set objWordApplication = GetObject(, "Word

How to “invoke” a class instance in PHP?

£可爱£侵袭症+ 提交于 2019-11-29 10:23:56
is there any possibility to "invoke" a class instance by a string representation? In this case i would expect code to look like this: class MyClass { public $attribute; } $obj = getInstanceOf( "MyClass"); //$obj is now an instance of MyClass $obj->attribute = "Hello World"; I think this must be possible, as PHP's SoapClient accepts a list of classMappings which is used to map a WSDL element to a PHP Class. But how is the SoapClient "invoking" the class instances? $class = 'MyClass'; $instance = new $class; However, if your class' constructor accepts a variable number of arguments, and you hold

What does 'is an instance of' mean in Javascript?

只愿长相守 提交于 2019-11-29 06:50:56
The answer to this question: What is the initial value of a JavaScript function's prototype property? has this sentence: The initial value of prototype on any newly-created Function instance is a new instance of Object As far as I know, Javascript doesn't have classes and the word 'instance' therefor doesn't make sense in my head. How should one interpret 'instance' in Javascript? Sorry, I don't have enough rep to put my question in the comment thread on that answer. You're right that JavaScript doesn't have classes (yet), but it does have constructor functions, an instanceof operator that

What is the correct ways to write Boto3 filters to use customise tag name?

不问归期 提交于 2019-11-29 06:15:01
I am trying to list the instances on tag values of different tag keys For eg> one tag key - Environment, other tag key - Role. My code is given below : import argparse import boto3 AWS_ACCESS_KEY_ID = '<Access Key>' AWS_SECRET_ACCESS_KEY = '<Secret Key>' def get_ec2_instances(Env,Role): ec2 = boto3.client("ec2", region) reservations = ec2.describe_instances(Filters={"tag:environment" : Env, "tag:role" : Role}) for reservation in reservations["Reservations"] : for instance in reservation["Instances"]: print "%s" % (instance.tags['Name']) if __name__ == '__main__': regions = ['us-east-1','us

Create an instance of a React class from a string

为君一笑 提交于 2019-11-29 05:35:54
I have a string which contains a name of the Class (this is coming from a json file). This string tells my Template Class which layout / template to use for the data (also in json). The issue is my layout is not displaying. Home.jsx: //a template or layout. var Home = React.createClass({ render () { return ( <div>Home layout</div> ) } }); Template.jsx: var Template = React.createClass({ render: function() { var Tag = this.props.template; //this is the name of the class eg. 'Home' return ( <Tag /> ); } }); I don't get any errors but I also don't see the layout / Home Class. I've checked the

GAE Go - “This request caused a new process to be started for your application…”

耗尽温柔 提交于 2019-11-29 02:59:28
问题 I've encountered this problem for a second time now, and I'm wondering if there is any solution to this. I'm running an application on Google App Engine that relies on frequent communication with a website through HTTP JSON RPC. It appears that GAE has a tendency to randomly display a message like this in the logs: "This request caused a new process to be started for your application, and thus caused your application code to be loaded for the first time. This request may thus take longer and