class

Inner class function without self

做~自己de王妃 提交于 2020-07-15 03:09:08
问题 Peace, everyone! I'm using Python 3.6.3 and I find strange that such construction is possible: class TestClass(object): def __init__(self): self.arg = "arg" def test(): print("Hey test") And using: >>> TestClass.test() "Hey test" I know that in Python there are standard methods with self as parameter (don't know how to call them properly), static methods, class methods, abstract methods. But what kind of method the test() is? Is it static method? Edited: Are there any useful usecases of such

How to return a class instance in member function of a class?

℡╲_俬逩灬. 提交于 2020-07-11 05:14:36
问题 I want to return a class instance in member function of a class, my code is: class MyClass(object): def __init__(self, *args, **kwargs): [snippet] def func(self, *args, **kwargs): [snippet] return class_instnace_of_MyClass if __name__ == '__main__': obj = MyClass(...) newobj = obj.func(...) # type(newobj) is MyClass I think I can call __init__() in func() , and return a new instance of MyClass, but I don't think it is a Pythonic way to do so. How should I do that? Thank you! 回答1: I feel like

How to return a class instance in member function of a class?

假如想象 提交于 2020-07-11 05:13:14
问题 I want to return a class instance in member function of a class, my code is: class MyClass(object): def __init__(self, *args, **kwargs): [snippet] def func(self, *args, **kwargs): [snippet] return class_instnace_of_MyClass if __name__ == '__main__': obj = MyClass(...) newobj = obj.func(...) # type(newobj) is MyClass I think I can call __init__() in func() , and return a new instance of MyClass, but I don't think it is a Pythonic way to do so. How should I do that? Thank you! 回答1: I feel like

DIfferent ways exist to create object attributes outside of init method based on the initial attributes

℡╲_俬逩灬. 提交于 2020-07-10 06:33:34
问题 I'm trying to understand OOP (specifically, in Python 3). Here's a basic class template: class Lines: """ Arguments: list of coordinates """ def __init__(self, points): self.x1 = points[0] self.y1 = points[1] self.x2 = points[2] self.y2 = points[3] I pass starting and ending (x,y) coordinates in a list to the class. However, I want to also add a length , slope , and y-intercept attribute to the objects of this class Lines ( Note : I do not want them as methods). Here are a couple ways I found

DIfferent ways exist to create object attributes outside of init method based on the initial attributes

[亡魂溺海] 提交于 2020-07-10 06:33:18
问题 I'm trying to understand OOP (specifically, in Python 3). Here's a basic class template: class Lines: """ Arguments: list of coordinates """ def __init__(self, points): self.x1 = points[0] self.y1 = points[1] self.x2 = points[2] self.y2 = points[3] I pass starting and ending (x,y) coordinates in a list to the class. However, I want to also add a length , slope , and y-intercept attribute to the objects of this class Lines ( Note : I do not want them as methods). Here are a couple ways I found

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

Default get method that returns specific property - MATLAB

可紊 提交于 2020-07-09 06:50:53
问题 I'm in the process of refactoring some MATLAB legacy software involving data obtained during a broad set of tests. I'm trying to create a class that contains the data of each individual channel, together with some extra info (e.g. its physical units). Just for the sake of placing this question here, the class could look like this: classdef Channel < handle properties (Access = 'private') prvValue, prvUnits; end properties (Dependent) value, units; end methods function this = Channel(value,

How can I conditionally instantiate an object?

安稳与你 提交于 2020-07-09 04:33:17
问题 I'm trying to do some conditional work like so: Type object; if (cond) { doSomeStuff(); object = getObject(); doMoreStuff(); } else { doSomeOtherStuff(); object = getDifferentObject(); doEvenMoreStuff(); } use(object); The only way I can think of solving this is the duplicate the use code (which is actually inline code in my application) and declare object in each branch of the if block. If I wanted to avoid duplicate code I'd have to wrap it in some use function, as I have above. In a real

Istream function to read with istream parameter

本小妞迷上赌 提交于 2020-07-07 06:58:17
问题 I'm trying to understand this code istream &read(istream &is, Sales_data &item) { double price = 0; is >> item.bookNo >> item.units_sold >> price; item.revenue = price * item.units_sold; return is; } ostream &print(ostream &os, const Sales_data &item) { os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price(); return os; } I don't understand what are these function , also I can't understand why we use istream for reading and ostream for printing instead of

Istream function to read with istream parameter

ⅰ亾dé卋堺 提交于 2020-07-07 06:58:10
问题 I'm trying to understand this code istream &read(istream &is, Sales_data &item) { double price = 0; is >> item.bookNo >> item.units_sold >> price; item.revenue = price * item.units_sold; return is; } ostream &print(ostream &os, const Sales_data &item) { os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price(); return os; } I don't understand what are these function , also I can't understand why we use istream for reading and ostream for printing instead of