subclassing

Write class such that calling instance returns all instance variables

ⅰ亾dé卋堺 提交于 2021-02-11 12:46:49
问题 I have answered my own question - see answer below I'm writing a class, and I want this behavior: a = f(10,20) some_funct(a.row) # some_function is given 10 some_funct(a.col) # some_function is given 20 some_funct(a) # some_function is given a tuple of 10, 20 <-- THIS ONE :) The last behavior is stumping me. I have not seen any examples that cover this. Thus far: class f(object): """Simple 2d object""" row: int col: int def __init__(self, row, col): self.row = row self.col = col Explictly I

Write class such that calling instance returns all instance variables

旧街凉风 提交于 2021-02-11 12:45:26
问题 I have answered my own question - see answer below I'm writing a class, and I want this behavior: a = f(10,20) some_funct(a.row) # some_function is given 10 some_funct(a.col) # some_function is given 20 some_funct(a) # some_function is given a tuple of 10, 20 <-- THIS ONE :) The last behavior is stumping me. I have not seen any examples that cover this. Thus far: class f(object): """Simple 2d object""" row: int col: int def __init__(self, row, col): self.row = row self.col = col Explictly I

unexpected keyword argument 'sample_weight' when sub-classing tensor-flow loss class (categorical_crossentropy) to created a weighted loss function

China☆狼群 提交于 2021-02-07 20:18:35
问题 Struggling to get a sub-classed loss function to work in Tensorflow (2.2.0). Initially tried this code (which I know has worked for others - see https://github.com/keras-team/keras/issues/2115#issuecomment-530762739): import tensorflow.keras.backend as K from tensorflow.keras.losses import CategoricalCrossentropy class WeightedCategoricalCrossentropy(CategoricalCrossentropy): def __init__(self, cost_mat, name='weighted_categorical_crossentropy', **kwargs): assert(cost_mat.ndim == 2) assert

Subclassing int and overriding the __init__ method - Python [duplicate]

爷,独闯天下 提交于 2021-02-05 06:00:47
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: inheritance from str or int Hi folks, I'm trying to subclass the int class without any success. Here is my attempt: class SpecialInt(int): def __init__(self, x, base=10, important_text=''): int.__init__(self, x, base) self.important_text=important_text If I perform the following: integer = SpecialInt(123, 10, 'rage of the unicorns') I get this error: TypeRror: int() takes at most 2 arguments (3 given) Any ideas?

When subclassing ndarray why does a transpose happen after __array_finalize__ and not before?

萝らか妹 提交于 2021-01-22 08:36:01
问题 Let us for simplicity just copy the diagnostic ndarray subclass from the numpy docs: import numpy as np class MySubClass(np.ndarray): def __new__(cls, input_array, info=None): obj = np.asarray(input_array).view(cls) obj.info = info return obj def __array_finalize__(self, obj): print('In __array_finalize__:') print(' self is %s' % repr(self)) print(' obj is %s' % repr(obj)) if obj is None: return self.info = getattr(obj, 'info', None) Now let's do a simple example: >>> x = MySubClass(np.ones(

Override .T (transpose) in subclass of numpy ndarray

允我心安 提交于 2020-08-26 07:57:06
问题 I have a three dimensional dataset where the 1st dimension gives the type of the variable and the 2nd and 3rd dimensions are spatial indexes. I am attempting to make this data more user friendly by creating a subclass of ndarray containing the data, but with attributes that have sensible names that point to the appropriate variable dimension. One of the variable types is temperature, which I would like to represent with the attribute .T . I attempt to set it like this: self.T = self[8,:,:]

Calling __new__ when making a subclass of tuple [duplicate]

╄→гoц情女王★ 提交于 2020-06-10 23:38:52
问题 This question already has answers here : Why is __init__() always called after __new__()? (18 answers) Closed 4 years ago . In Python, when subclassing tuple, the __new__ function is called with self as an argument. For example, here is a paraphrased version of PySpark's Row class: class Row(tuple): def __new__(self, args): return tuple.__new__(self, args) But help(tuple) shows no self argument to __new__ : __new__(*args, **kwargs) from builtins.type Create and return a new object. See help

Calling __new__ when making a subclass of tuple [duplicate]

谁都会走 提交于 2020-06-10 23:31:32
问题 This question already has answers here : Why is __init__() always called after __new__()? (18 answers) Closed 4 years ago . In Python, when subclassing tuple, the __new__ function is called with self as an argument. For example, here is a paraphrased version of PySpark's Row class: class Row(tuple): def __new__(self, args): return tuple.__new__(self, args) But help(tuple) shows no self argument to __new__ : __new__(*args, **kwargs) from builtins.type Create and return a new object. See help

Python subclassing a class with custom __new__

微笑、不失礼 提交于 2020-02-03 23:41:13
问题 There's a class (not created by me, from a 3rd party library) that has no __init__ declared (other than object 's __init__ ), and this is its __new__ : def __new__(cls, uid): self = super().__new__(cls, uid) self._info = get_info_from_uid(uid) return self I can't see why didn't they just use __init__ here, but they didn't. Now I'd like to subclass this and give it an additional attribute; before I checked the source-code of the class (only the documentation), I thought it's just using __init_