python object attributes and methods

2024/10/5 17:23:01

In python all data is object and any object should have attributes and methods. Does somebody know python object without any attributes and methods?

>>> len(dir(1))
64
Answer

This is easy to accomplish by overriding __dir__ and __getattribute__:

class Empty(object):def __dir__(self):return []def __getattribute__(self, name):raise AttributeError("'{0}' object has no attribute '{1}'".format(type(self).__name__, name))e = Empty()
dir(e)
[]
e.__name__
AttributeError: 'Empty' object has no attribute '__name__'

(In python2, Empty needs to be a new-style class, so the class Empty(object): is required; in python3 old-style classes are extinct so class Empty: is sufficient.)

https://en.xdnf.cn/q/119906.html

Related Q&A

How to retrieve nested data with BeautifulSoup?

I have the below webpage source: </li><li class="cl-static-search-result" title="BELLO HONDA ACCORD &quot;95 MIL MILLAS&quot;. REALMENTE COMO NUEVO"><a href=&…

applying onehotencoder on numpy array

I am applying OneHotEncoder on numpy array.Heres the codeprint X.shape, test_data.shape #gives 4100, 15) (410, 15) onehotencoder_1 = OneHotEncoder(categorical_features = [0, 3, 4, 5, 6, 8, 9, 11, 12]) …

How to delete temp folder data using python script [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 6…

Save a list of objects on exit of pygame game [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 10 years ago.Improv…

Trying to make loop for a function that stops after the result is lower than a certain value

Im taking a beginner python class and part of an exercise we were given was this:The point x with the property x= sin(x)−ax+ 30 is called a fixed point of the function f(x) = sin(x)−ax+ 30. It can b…

python url extract from html

I need python regex to extract urls from html, example html code :<a href=""http://a0c5e.site.it/r"" target=_blank><font color=#808080>MailUp</font></a> <…

Regex match each character at least once [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

How to cluster with K-means, when number of clusters and their sizes are known [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 1…

Converting German characters (like , etc) from Mac Roman to UTF (or similar)?

I have a CSV file which I can read in and at all works fine except for the specific German (and possibly other) characters. Ive used chardet to determine that the encoding is Mac Roman import chardetde…

Caesar cipher without knowing the Key

Hey guys if you look at my code below you will be able to see that i was able to create a program that can open a file decode the content of the file and save it into another file but i need to input t…