class

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

What is the meaning of macro in front of class definition in c++

泪湿孤枕 提交于 2020-06-09 05:22:06
问题 What is the syntax in C++ that allows the following construction, where a word appears in between "class" and "class_name"? namespace octave { // Command line arguments. See also options-usage.h. class OCTINTERP_API cmdline_options { public: ... Note, I am not asking the meaning of the macro. I am not asking what it does. I am not asking if it is empty. I am asking about the syntax of class definition. Several sources explain the syntax, but without the word in the middle, for example: class

Trying to grasp the point of putting frames and widgets in classes for tkinter

允我心安 提交于 2020-06-08 15:06:21
问题 As the question states, I can't seem to fully grasp the point of using classes with tkinter. I have read through a decent number of different sites but I keep getting search results on how to create and use classes, but none so far have been able to get through to me. I've even scoured through the suggested questions while asking this one. The closest I've come to understanding is Bryan's explanation on this answer to the question Why use classes when programming a tkinter gui? But still, I

Use dependency injection in static class

丶灬走出姿态 提交于 2020-06-08 04:52:07
问题 I need to use Dependency Injection in a static class. the method in the static class needs the value of an injected dependency. The following code sample demonstrates my problem: public static class XHelper { public static TResponse Execute(string metodo, TRequest request) { // How do I retrieve the IConfiguracion dependency here? IConfiguracion x = ...; // The dependency gives access to the value I need string y = x.apiUrl; return xxx; } } 回答1: You basically have two options: Change the

C-like Static Variable inside a Python class method

落花浮王杯 提交于 2020-05-31 05:01:06
问题 After 20 years of C++ experience I am struggling to learn something of Python. Now I'd like to have a method (a function inside a class) that has a "static" variable of its own, and not a static class variable. Probably a pseudo code example can illustrate better what I want. class dummy: @staticmethod def foo(): foo.counter += 1 print "You have called me {} times.".format(foo.counter) foo.counter = 0 NOTE 1: I used @staticmethod just for simplicity, but this is irrelevant. NOTE 2: This

C-like Static Variable inside a Python class method

无人久伴 提交于 2020-05-31 05:01:06
问题 After 20 years of C++ experience I am struggling to learn something of Python. Now I'd like to have a method (a function inside a class) that has a "static" variable of its own, and not a static class variable. Probably a pseudo code example can illustrate better what I want. class dummy: @staticmethod def foo(): foo.counter += 1 print "You have called me {} times.".format(foo.counter) foo.counter = 0 NOTE 1: I used @staticmethod just for simplicity, but this is irrelevant. NOTE 2: This

How to create a class that extends the `int` base class that has input validation?

☆樱花仙子☆ 提交于 2020-05-30 09:09:27
问题 I'd like to make a class that extends the int base class, so that the object itself is an integer (i.e. you set it and read from it directly), but also has input validation - for example, only allow a given range. From what I have researched, the __init__ method is not called when you extend normal base classes, so I'm not sure how to do this. I'm also not clear on how you access the value of the object (i.e. the actual integer assigned to it) or modify that value from within the class. I see

How to create a class that extends the `int` base class that has input validation?

别说谁变了你拦得住时间么 提交于 2020-05-30 09:09:01
问题 I'd like to make a class that extends the int base class, so that the object itself is an integer (i.e. you set it and read from it directly), but also has input validation - for example, only allow a given range. From what I have researched, the __init__ method is not called when you extend normal base classes, so I'm not sure how to do this. I'm also not clear on how you access the value of the object (i.e. the actual integer assigned to it) or modify that value from within the class. I see

Repeating decorators for instance variables

故事扮演 提交于 2020-05-30 08:04:00
问题 I am writing a class where I need to check whether the instance variables are of a certain type. I noticed that there is a lot of repeating code. Is there a better way to do similar checks on the instance variables? Or is this the right way to do it? class Variable(): __type = 'Variable' def __init__(self, id = None, updateable = True, name = 'Variable', value=None): if id is not None: self.id = id if value is not None: self.value = value self.updateable = updateable self.name = name