python-object

Can I declare Python class fields outside the constructor method?

吃可爱长大的小学妹 提交于 2020-04-11 07:38:25
问题 I am absolutly new in Python (I came from Java) and I have the following doubts about class fields. Considering a code like this: class Toy(): def__init__(self, color, age): self.color = color self.age = age action_figure = Toy('red', 10) Ok, what is done it is clear and very simple: it is defining a Toy class. In the constructor method is defining two fields and how to set the fields value. Finnally (in the "main") it is created a new Toy instance passing the values of the field in the

Question about multiple inheritance, dynamic class creating and instantiation

别来无恙 提交于 2020-03-23 16:55:51
问题 Hello I have the following situation: A specialized class that inherits from two parent class The need to define the most specialized class at run time, based on some information that I get only when I start reading data from a database. I defined the following code to handle the create all the classes in the chain: class BusinessDocument(): @staticmethod def get_class(doc_type): switch = { 'MasterData': MasterData, 'Transactional': Transactional } func = switch.get(doc_type, lambda: "Invalid

Setting a class __name__ declaratively

纵然是瞬间 提交于 2019-12-30 09:10:10
问题 Why can't you override a class name declaratively, e.g. to use a class name which is not a valid identifier? >>> class Potato: ... __name__ = 'not Potato' ... >>> Potato.__name__ # doesn't stick 'Potato' >>> Potato().__name__ # .. but it's in the dict 'not Potato' I thought maybe it was simply a case that this was overwritten after the class definition block completes. But seems that's not true, because the name is writable yet apparently not set in the class dict: >>> Potato.__name__ = 'no

Get a class/instance only declared attributes(not inherited)?

北战南征 提交于 2019-12-25 02:24:10
问题 I have 3 classes A,B,C , C inherting form A and B: class A: a = "ala" class B: b = "bla" class C(A,B): c = "cla" How can I get only the Attributes of C, attributes that are not inherited ? 回答1: You could access the __dict__ of C directly via the vars builtin. >>> vars(C)['c'] 'cla' >>> vars(C)['b'] ... KeyError: 'b' There's not much more to say without further context about what your real problem is. 来源: https://stackoverflow.com/questions/52127112/get-a-class-instance-only-declared

How to extend Symbol class in sympy?

限于喜欢 提交于 2019-12-24 07:25:18
问题 I’m having trouble extending the Symbol class in sympy. It could be a result of something with class extensions in general, or it might also be an issue with this specific “Symbol” class. I want to extend the Symbol class to have an additional attribute called “boolean_attr” which is a True/False attribute. This simulates what I’m trying to do: class A(object): # This simulates what the "Symbol" class is in sympy __slots__ = ['a'] def __init__(self, a): self.a = a # this simulates my

'super' object has no attribute '__eq__'

泪湿孤枕 提交于 2019-12-08 15:31:58
问题 When I try to override the magic method __eq__ , and use super to access the base method found in object , I get an error. There's no way this is a bug, but it sure feels like one: class A(object): def __eq__(self, other): return super(A, self).__eq__(other) A() == 0 # raises AttributeError: 'super' object has no attribute '__eq__' This is unintuitive because object.__eq__ exists, but for class A(object): pass it doesn't. If I'm not mistaken __eq__ resorts to an is check, so that may be the