Questions tagged [hasattr]

38 questions
0
votes
2 answers

In Python, how do I get a variable via a string representation of the name of the variable?

I'm using Python 2.7. Take, for example, the following code: class Apple: def __init__(self, n): self.n = n def printVariable(self, s): # print variable named s if hasattr(self, s): print ... What would I replace…
user3745189
  • 521
  • 1
  • 7
  • 17
0
votes
2 answers

For loop and Storing variables

I have a long list of if statements that are checking if an object (self) contains a series of optional attributes. As an example: if(hasattr(self, variableList)): var1 = self.var1 if(hasattr(self, "var2")): var2 = self.var2 …
Xivilai
  • 2,481
  • 3
  • 15
  • 15
0
votes
1 answer

Check whether class variable has been defined

I am dealing with a scenario where I have a python class Foo. Foo, among other things does many big calculations, which I would not do unless required. So, when I define the getter method for one of those big calculations, how do I make sure that…
0
votes
1 answer

Python: Loop with possible AttributeErrors

I have an array row. I want to add a set of attributes of an object actor to to the array. However, not all actors have each of the attributes. I want to run a loop that will add the attribute if it exists, but add 'none' if it does not. I can't…
David Y. Stephenson
  • 872
  • 4
  • 22
  • 44
0
votes
1 answer

Method 'hasattr' not testing property correctly?

I'm really stuck with this! I'm getting some product data from Amazon, which is returned as an XML. When the search keyword is valid, it returns the XML with nodes Items > Item. With a wrong keyword, or a keyword that doesn't return valid results,…
Rutwick Gangurde
  • 4,772
  • 11
  • 53
  • 87
-1
votes
1 answer

Is hasattr() a method?

Is hasattr() a method? It does take an object instance as a parameter, but it is not used as object.hasttr(). I would say it is not a method then?
Nicolas Berthier
  • 459
  • 1
  • 8
  • 17
-1
votes
2 answers

hasattrr to check properties of a object in python

I am writing web services using Python flask. I have created a class and loading its property names and values from user input (json data provided with service call). from flask import json class DataSetContract(object): """description of…
Abhash786
  • 881
  • 2
  • 24
  • 53
-2
votes
3 answers

How to reset a variable assigned to function in Python 2.73?

so basically a guy helped me improve my code. The problem is, it's still dead disappointing and does not work. What I wanna do, is reset the lenRecur.number so I could use the function again, using other strings and getting correct answers (not too…
5brickbomzh
  • 99
  • 1
  • 4
  • 12
1 2
3