deque creation from vector class in python [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I define a deque and insert a vector in it in python.My goal was define a deque from vectors.
But when I iterate on this queue,python returns first argument of this vector.
How can I define a deque from vector?

I'm not sure what you mean by vector, but, as with about any sequence in Python you are able to store any type of object in it. Unlike C++, where the type of the stored objects needs to be known at compile time.
Here is an example:
class vector(object):
def __str__(self):
return "I'm a vector, for realz!"
...
mydeque = deque()
for i in range(1, 20):
mydeque.append(vector())
for vec in mydeque:
print(vec)

Related

Is it possible to create a class ->Add, instance --> obj1 that can be called multiple times like the following : obj1(12)(34)(23)? [duplicate]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 days ago.
Improve this question
How should I initialize multiple class arguments that came as chain and then calculate sum of them?
I've tried many ways but NOTHING
Do you have any idea?
>>> Chain(2.5)(2)(2)(2.5) # sum
9
>>> Chain(3)(1.5)(2)(3) # sum
9.5
In general, you'll want to add a __call__ method to your class so that calling an instance returns a new instance. Your class should also subclass the type matching the result you want.
In this specific case, the new instance could, for example, maintain a running sum of the initial value and all successive arguments.
class Chain(float):
def __call__(self, x):
return Chain(self + x)
Then
>>> Chain(2.5)
2.5
>>> Chain(2.5)(2)
4.5
>>> Chain(2.5)(2)(2)
6.5
>>> Chain(2.5)(2)(2)(2.5)
9.0

Re-usable function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am using Python for some operations on a XML file.
Because I am new to programming I would like to know how I can re-use the snippet below, currently it has a hard-coded statement in it.
Please look at the line with
for ERPRecord in aroot.iter('part'):
inside it, aroot should be replaced with the modular option or variable.
def SetERP(ArticleN,ERPn):
for ERPRecord in aroot.iter('part'):
if ERPRecord.get('P_ARTICLE_ORDERNR') == ArticleN:
ERPRecord.set('P_ARTICLE_ERPNR', ERPn)
I would like to have a function without hard-coded parts in so it is able to be used again in other projects. My best guess is that the sequence "aroot" will be replaced by a variable like this:
def SetERP(ArticleN,ERPn, XMLroot):
for ERPRecord in XMLroot.iter('part'):
if ERPRecord.get('P_ARTICLE_ORDERNR') == ArticleN:
ERPRecord.set('P_ARTICLE_ERPNR', ERPn)
Any advice on this would be welcome!
You could define aroot as a parameter, so you would have to pass your root in every time you call the function, if that is what you mean?
def SetERP(ArticleN, ERPn, aroot):
for ERPRecord in aroot.iter('part'):
if ERPRecord.get('P_ARTICLE_ORDERNR') == ArticleN:
ERPRecord.set('P_ARTICLE_ERPNR', ERPn)

What is the benefit to reference a method in a class? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I found code on GitHub that is similar to my ExampleClass, but I don't understand the benefit of using getx = calcualte_x. I feel like it is somewhat close to #property, but it is not obviously.
what is the benefit of that line and why not just use class-instance.calcualte_x(add) instead of class-instance.getx(add)
class ExampleClass():
def __init__(self, x_val):
self.x = x_val
def calcualte_x(self, add):
x = self.x + add
getx = calcualte_x
It can be an alias - for ease of use or for backward compatibility for example.

Access value of function attribute in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Suppose I have a function -
def foo(x,y):
pass
a = foo(5,6)
How do I access the values 5 and 6 from a?
From the code you have shown us, you cannot -- 5 and 6 were passed in to foo, you didn't keep a copy of them, foo didn't keep a copy of them, so they are gone.
So, as the above paragraph hinted, somebody has to keep a copy of those arguments if you want to do something else with them later, and while it is possible to have a function do so, that's not really what they are intended for. So your easy options are:
make foo a class that saves the arguments it was called with (which is still highly unusual), or
save the arguments yourself (arg1, arg2 = 5, 6 for example)
You can't. You'd need to use an object.

Why python does not update an external parameter with assignment? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Can anyone explain the reason why python behaves differently in the following two cases please? Much appreciation.
def modifyNone(x):
print("B4:"+str(x))
# x.append(5)
x=[5]
print("In:"+str(x))
a = []
modifyNone(a)
print("After:"+str(a))
Output:
B4:[]
In:[5]
After:[]
Method:
def modifyNone(x):
print("B4:"+str(x))
x.append(5)
# x=[5]
print("In:"+str(x))
a = []
modifyNone(a)
print("After:"+str(a))
Output:
B4:[]
In:[5]
After:[5]
Python is pass by value, so you'll have to reassign a returned value like so:
a = modifyNone(a) # where the function returns a value
You don't seem to understand variable scope as well, try the documentation.

Categories