Questions tagged [hasattr]
38 questions
2
votes
2 answers
hasattr with functions
How to check attr existence in function or method with hasattr (or without)?
When I try to check it is False in any way:
>>> def f():
at = True
>>> hasattr(f, 'at')
False
>>> hasattr(f(), 'at')
False

I159
- 29,741
- 31
- 97
- 132
1
vote
1 answer
Why an object can be split into 2 components
I came across this class Vector2 which was constructed like this
class Vector2:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
if hasattr(x, "__getitem__"):
x, y = x
self._v = [float(x),…

Nemo
- 1,124
- 2
- 16
- 39
1
vote
1 answer
hasattr(value, "contribute_to_class") returns KeyError: 'contribute_to_class' (Django 4.1)
A Django 1.1 / Python 2.7 project that I'm attempting to run on Python 3.10 / Django 4.1.
A Python related error (i.e. old import) or a django code error (i.e. missing field that's now mandatory) pops up, I fix it and rerun.
The current error,…

Jay
- 2,535
- 3
- 32
- 44
1
vote
2 answers
Function 'hasattr()' doesn't work as I expected in Python
Function 'hasattr()' doesn't work as I expected in Python
I have the following code:
#!/usr/bin/python
import re
import os
import sys
results=[{'data': {}, 'name': 'site1'}, {'data': {u'Brazil': '5/1', u'Panama': '2000/1'}, 'name': 'site2'}]
print…

TigerTV.ru
- 1,058
- 2
- 16
- 34
1
vote
0 answers
Why does lib.__dict__(vars) only show an object after runnning hasattr or getattr?
Here is the example code:
import ctypes.util
from ctypes import CDLL
path = ctypes.util.find_library('crypto')
lib = CDLL(path)
hasattr(lib, 'EVP_get_cipherbyname') #only run this line or getattr,
#the print(lib.__dict__) show the…

Edwin
- 71
- 1
- 1
- 3
1
vote
1 answer
Beautifulsoup and Soupstrainer for getting links dont work with hasattr, returning always true
i am using Beautifulsoup4 and Soupstrainer with Python 3.3 for getting all links from a webpage. The following is the important code-snippet:
r = requests.get(adress, headers=headers)
for link in BeautifulSoup(r.text, parse_only=SoupStrainer('a')):
…

zwieback86
- 387
- 3
- 7
- 14
1
vote
2 answers
Duck typing and use of hasattr
I've seen lots of times something like:
def parse(text):
if hasattr(text, 'read'):
text = text.read()
# Parse the text here...
but if I pass an instance of the following class, it will surely fail:
class X(object):
def…

Oscar Mederos
- 29,016
- 22
- 84
- 124
1
vote
2 answers
Python reference for hasattr() choices to identify types
Is there a good one-stop-shop Python reference for choosing attributes to use with hasattr() to identify types.
For example, the following is for a sequence which is not a string:
def is_sequence(arg):
return (not hasattr(arg, "strip") and
…

Michael Allan Jackson
- 4,217
- 3
- 35
- 45
0
votes
1 answer
Python difference between hasattr and has_attr
Despite the "similarly phrased" warning, I don't think this has been asked.
I'm starting to use BeautifulSoup (v4) and, for example, to get the href from an A-link you might do this:
for a_link in soup.html.body.select( 'a' ):
print( a_link )
…

mike rodent
- 14,126
- 11
- 103
- 157
0
votes
0 answers
hasattr() that previously returned "True", returns "False" after code converted into cython
I create a simple class in python and check whether it has a certain attribute. After hasattr() Return True I try to rewrite it in Cython and but then hasattr() returns False.
Look at this example in python:
class Foo_p:
def __init__(self,…

Oran Szachter
- 11
- 1
0
votes
1 answer
Using getattr to call a function in a separate class
I may be trying to do something that is outside of the realm of possibility here, but I figured I would ask first before abandoning hope. So here it goes...
I have 2 classes, A and B. Each class has an arbitrary number of functions. Class B will be…

Corey Gomez
- 13
- 4
0
votes
1 answer
How to return value with dynamic method calls through getattr()
I have a class that is called that runs a while loop command prompt, i am using dir() and getattr() to dynamically create a list of methods for a command shell. I want to return values, but return from a dynamically called method just exits to main…

Aesycos
- 3
- 1
- 5
0
votes
1 answer
hasattr returns always True for app engine ndb entity
I am applying this answer to my project
This is my ndb entity where is_deleted added later.
class FRoom(ndb.Model):
location = ndb.StringProperty(default="")
is_deleted = ndb.BooleanProperty(default=False) #added later
#other…

asdf_enel_hak
- 7,474
- 5
- 42
- 84
0
votes
3 answers
How to check submodules in Python with hasattr
At runtime, the Python code gets the name of a submodule to load, which I don't know before. Now, I want to check, if this submodule exists inside an existing module. Consider this structure, where foo and bar can be specified:
master/
|
|-…

Boldewyn
- 81,211
- 44
- 156
- 212
0
votes
1 answer
how to check whether an image have alt attribute or not in jquery
i want to add one Class to an image if it have alt attribute and one class to an image which doesnot have an alt attribute.
i tried this but its not working
if ($(img).hasAttr('alt'))
{
this.addClass("haveAlt");
}

simr
- 9
- 2