Does separate method by argument? [closed] - python

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
Could you give me an opinion which A or B is better?
I sometimes get lost in the definition of method.
# Both returns same value.
# Only the arguments are difference.
# ------------------
# pattern A
def some_method_by_name(name: str):
record = <SELECT ... WHERE record_name=str>
return record
def some_method_by_id(id: int):
record = <SELECT ... WHERE record_id=id>
return record
# ------------------
# pattern B
def some_method(**kwargs):
if 'id' in kwargs:
record = <SELECT ... WHERE record_id=kwargs.get('id')>
elif 'str' in kwargs:
record = <SELECT ... WHERE record_name=kwargs.get('str')>
else:
raise Exception('wrong!')
return record
I think that 'A' is good because to separate method by arguments is easy to understand, but I think that it is verbose.
Thanks.

Related

Sum of items of a class [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 yesterday.
Improve this question
What I have to do is to create a class that counts from a giving single argument and itself make the arithmetical operation.
class Counts:
def __init__(self, value=0):
self.value = value
def newvalue(self, other):
return Counts(self.value + other)
But for every I make to the code I got any different error, either syntax or callable argument.
The idea is to get
Counts()
Expected output
0
Next
Counts.newvalue(10)
Expected output
10
Next
Counts.newvalue(40)
Expected output
50
Next
Counts.newvalue(-17)
Expected output
33
And so on.
The code that shows the expected behaviour is
class Counts:
value = 0
def __new__(self):
return self.value
#classmethod
def newvalue(cls, other):
cls.value += other
return cls.value
however this is a somewhat strange piece of code, as you are creating a class that returns a value when initialized instead of an object deriving from that class by overriding __new__, which is pretty non-standard.
also if you want to zero the value whenever Count() is called, you can add a self.value = 0 before the return self.value
Tests ->
print(Counts())
print(Counts.newvalue(10))
print(Counts.newvalue(40))
print(Counts.newvalue(-17))
returns
0
10
50
33

<__main__.Employee object at 0x000001E32362C1F0> [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 1 year ago.
Improve this question
class Employee:
def __init__(self,first,last,salary):
self.first = first
self.last = last
self.salary = salary
emp_1 = Employee("Rohan","Parab",100000)
print(emp_1)
You need to implement the __str__ method otherwise it will print (as you may have observed) the object location.
For example:
def __str__(self):
return f"First Name: {self.first}, Last Name {self.last}, Salary {self.salary}"

How to change value dict out class [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I want to change dict value, but allert me a Error 'object does not support item assignment'
Full Code:
def __init__(self):
self.dict = dict()
def __getitem__(self, item):
return self.dict[item]
arr = SparseArray()
arr[0] = 1```
You to override __setitem__
def __setitem__(self, key, value):
self.dict[key] = value
Then you'll be able to assign a value using an index
# use of __setitem__
arr[0] = 1
arr["foo"] = 5
# use of __getitem__
print(arr[0]) # 1
print(arr["foo"]) # 5
Also avoid naming variable as built-in name like dict, at least you can use items or values if there is no real sense about the variable's content

Inside class access to outside class variable? [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 4 years ago.
Improve this question
I have classes: C1 and C2, a C1 instance is created inside C2, and some C1 class functions also need variables from C2. Although I can move those function inside C2, it would be a little bit messy. Is there any way to import C2's variable into the test_func in C1?
C1_class.py:
class C1():
C1_a = 1
C1_b = 2
def test_func(self):
result = self.C1_a*C2.C2_a+self.C1_b*C2.C2_b
return result
C2_class.py
import C1
class C2():
C2_a = 1
C2_b = 2
C1_sample = C1_class.C1()
C2_sample = C2()
print(C2_sample.C1_sample.test_func)
Printing the function isn't what you want, I think ... you want the functional value, correct?
print(C2_sample.C1_sample.test_func() )
# ^^ forgot the parentheses
Output:
5
The problem is not that they're in two different files. It's that you haven't called the function.
test_func
is the function descriptor, giving you access to that object.
It's attributes include the parameters, etc.
test_func()
is a call to the function, returning a value.

How do you get a column name and row from table? [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 8 years ago.
Improve this question
I have to write a code where table is represented as dictionary, each key is the name of the column name and each value is the list of items in that column from top row to bottom.
I can use dictionary, list or whatever i want. So i decided to use dictionary
But when i test my code, I am getting this weird error :
>diction = {'a' : ['letter of a']}
>my_table = Table()
Traceback (most recent call last):
File "<string>", line 1, in <fragment>
builtins.TypeError: __init__() missing 3 required positional arguments: 'new_dict', 'coloumn_name', and 'coloumn_value'
Can someone help me with this?
class Table():
'''A class to represent a SQuEaL table'''
def __init__(self, new_dict, coloumn_name, coloumn_value):
self._new_dict = new_dict
self._key = coloumn_name
self._value = coloumn_value
def new_dictionary(self):
return self._new_dict
def get_coloumn_name(self):
return self._key
def get_coloumn_value(self):
return self._value
def set_dict(self, new_dict):
'''(Table, dict of {str: list of str}) -> NoneType
Populate this table with the data in new_dict.
The input dictionary must be of the form:
column_name: list_of_values
'''
# assign empty dictionary
self._new_dict = {}
# each key represents a coloumn name and each value is list of items
# that coloumn_name from top row to bottom(coloum_values)
self._new_dict += get_coloumn_name[self._key], get_coloumn_value[self._value]
return
You are getting this error because the __init__() function in your class requires 3 arguments - new_dict, coloumn_name, and coloumn_value - and you did not supply them.

Categories

Resources