subclass

Initialize subclass within class in python

核能气质少年 提交于 2020-07-10 04:53:51
问题 I am initializing a class along with two subclasses in Python using a dictionary. Would it be possible to check a key in the dictionary within the init and depending the result initialize either one of the two subclasses? For instance: Class Pet(): def __init__(self,dic): self.name=dic['name'] self.age=dic['age'] if dic['type']=='dog': #Initialize a dog which inherits all pet methods with name and age passed onto it elif dic['type']=='cat': #Initialize a dog which inherits all pet methods

python how to call static method from inside of a class body [duplicate]

我只是一个虾纸丫 提交于 2020-07-05 12:35:55
问题 This question already has answers here : Calling class staticmethod within the class body? (5 answers) Closed 2 years ago . Let's assume I have a class, with a static method, and I want a class property to be set to the value that this method returns: class A: @staticmethod def foo(): return 12 baz = foo() But doing this I get an error: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 5, in A TypeError: 'staticmethod' object is not callable I found a

create SKScene subclasses programmatically, without size info?

落花浮王杯 提交于 2020-06-22 12:05:22
问题 I'm trying to learn how to make a GameManager type class, and making individual classes for each of my GameScenes... probably the wrong thing to do, but for the sake of this question, please accept this as the way to do things. My GameManager looks like this, having a reference to each of the scenes, that's static: import SpriteKit class GM { static let scene2 = SecondScene() static let scene3 = ThirdScene() static let home = SKScene(fileNamed: "GameScene") } How do I create a SKScene

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

Is there a way to call a method on definition of a subclass in Python?

隐身守侯 提交于 2020-05-29 05:17:28
问题 The __init__ method defines what is done on creating an instance of a class. Can I do something equivalent when a subclass is created? Let's say I have the abstract class Entity : class Entity: def __onsubclasscreation__(cls): for var in cls.__annotations__: cls.__dict__[var] = property(lambda self:self.vars[var]) This would mean that whenever I define a new class inheriting from Entity , all annotated variables of that class would receive a getter: class Train(Entity): wagons: int color: str

Is there a way to call a method on definition of a subclass in Python?

天涯浪子 提交于 2020-05-29 05:15:29
问题 The __init__ method defines what is done on creating an instance of a class. Can I do something equivalent when a subclass is created? Let's say I have the abstract class Entity : class Entity: def __onsubclasscreation__(cls): for var in cls.__annotations__: cls.__dict__[var] = property(lambda self:self.vars[var]) This would mean that whenever I define a new class inheriting from Entity , all annotated variables of that class would receive a getter: class Train(Entity): wagons: int color: str

Python typing for a subclass of list

≡放荡痞女 提交于 2020-05-27 06:17:20
问题 I want to be able to define what the contents of a subclass of list have to be. The class would look like the following. class A(list): def __init__(self): list.__init__(self) I want to include typing such that the following would happen. import typing class A(list: typing.List[str]): # Maybe something like this def __init__(self): list.__init__(self) >> a = A() >> a.append("a") # No typing error >> a.append(1) # Typing error 回答1: typing conveniently provides a generic version of collections

Dictionary to dataclasses with inheritance of classes

萝らか妹 提交于 2020-05-17 06:06:21
问题 I have the following class @dataclass_json @dataclass class Input: sources: List[Sources] =None Transformations: List[str] =None As well as: @dataclass_json @dataclass class Source: type: str =None label: str =None path: str = None and the two subclasses: @dataclass_json @dataclass class Csv(Source): csv_path: str=None delimiter: str=';' and @dataclass_json @dataclass class Parquet(Source): parquet_path: str=None Given now the dictionary: parquet={type: 'Parquet', label: 'events', path: '/...

Subclassing datetime.datetime [duplicate]

北城余情 提交于 2020-05-15 11:10:31
问题 This question already has answers here : Why can't I subclass datetime.date? (6 answers) Closed 5 years ago . I'm looking into creating a python datetime-subclass which provides a default timezone when created. For the sake of keeping this question simple, let's assume I always want to hard-code my datetimes to be in UTC. I can't figure out why the following works: import datetime, dateutil.tz def foo(*args, **kwargs): kwargs['tzinfo'] = dateutil.tz.tzutc() return datetime.datetime(*args, *