I am improving this code to a student, here is the image: So I rewrite this using list compreenssion and oriented to a formal class, so the second question is: Can I improve yet more this code? Answer Yes you can improve your code to be more “Object Oriented”. It doesn’t make sence that Aluno contains a list, Aluno should
Tag: oop
Dynamically created classes with the same name
I’m trying to dynamically create subclasses in Python with type: and here’s what I see in the output: Obviously, this was not my intention. Two questions in that respect: How does Python handles multiple classes with identical names? What is a pythonic way to handle it? Of course, I can look up the name in the already existing subclasses, but
How to perform input validation for read-only instance attributes?
A very similar question was posted here, but there are no accepted answers, no code examples and I don’t really like the idea of using an external library as suggested by the only one answer provided there. The following code allows to define read-only instance attributes: but I would like to validate the user inputs as well. I want to
How do I convert a json file to a python class?
Consider this json file named h.json I want to convert this into a python dataclass. I could use an alternative constructor for getting each account, for example: but this is limited to what arguments (email, name, etc.) were defined in the dataclass. What if I were to modify the json to include another thing, say age? The script would end
how to apply a class function to replace NaN for mean within a subset of pandas df columns?
The class is composed of a set of attributes and functions including: Attributes: df : a pandas dataframe. numerical_feature_names: df columns with a numeric value. label_column_names: df string columns to be grouped. Functions: mean(nums): takes a list of numbers as input and returns the mean fill_na(df, numerical_feature_names, label_columns): takes class attributes as inputs and returns a transformed df. And here’s
How to override module name (__name__)?
I have two classes in separate files, a.py and b.py. I can not modify a.py in any way. The log output is: I want to change __name__ so that a child class’s call should log called in b. My project logs its module name as part of the output. However, when I inherit a class and call the parent class
How to trigger a method in 2 instances of the same class object in Python?
I had 2 instances (st0, st1) of the same class (State) in which has 2 methods. Somehow when st0.loc_st() is called, I want that st1.rem_st() to be automatically triggered. How can I achieve that purpose with Python OOP? Sorry the code looks a bit naive, but it’s a simplified model saves the reading effort. Answer If you want the method
how to pass variables to method
I want to pass variables to the method. which of the following is better? (x and y are constants) or: Answer I’d argue the second one is better with regards to function purity, though this is a principle of Functional Programming rather than Object Oriented Programming. The only thing I’d wonder then is why you’d even make it a member
a simple way to produces classes with global state
Suppose I have a class, defined, for example, thus: Now, I want to create a class which is identical to testclass except I don’t want testclass’ state cl, but rather its own individual state. Experiment shows that inheriting from testclass inherits cl, so that does not work. Any words of wisdom appreciated. Does this require the whole metaclass machinery? (which
AttributeError: ‘NoneType’ object has no attribute ‘sink’ [closed]
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers. Closed 1 year ago. Improve this question