class

In python, when self can be omitted?

不想你离开。 提交于 2021-01-21 11:27:09
问题 Codes below defined Duck class as composited from Bill class and Tail class. My questions is that as for the method about() inside Duck class definition, why can one write bill.description and tail.length ? Is self omitted here? If yes, when can I omit self ? Can I omit them inside __init__ method? class Bill(): def __init__(self, description): self.description = description class Tail(): def __init__(self, length): self.length = length class Duck(): def __init__(self, bill, tail): self.bill

In python, when self can be omitted?

二次信任 提交于 2021-01-21 11:26:40
问题 Codes below defined Duck class as composited from Bill class and Tail class. My questions is that as for the method about() inside Duck class definition, why can one write bill.description and tail.length ? Is self omitted here? If yes, when can I omit self ? Can I omit them inside __init__ method? class Bill(): def __init__(self, description): self.description = description class Tail(): def __init__(self, length): self.length = length class Duck(): def __init__(self, bill, tail): self.bill

Python - check for class existance

♀尐吖头ヾ 提交于 2021-01-21 09:14:53
问题 Is there a way to check if a class has been defined/exists? I have different options on a menu, and some of them require inherited variables, so if you try to select that function before you have set the variables, it crashes. My class is called Gen0, and I started it by simply putting class Gen0(): Followed by the rest of the code to set the variables. For context, I am doing a population model, so the initial values need to be set (option 1) before displaying (option 2), using (option 3) or

Python - check for class existance

送分小仙女□ 提交于 2021-01-21 09:14:23
问题 Is there a way to check if a class has been defined/exists? I have different options on a menu, and some of them require inherited variables, so if you try to select that function before you have set the variables, it crashes. My class is called Gen0, and I started it by simply putting class Gen0(): Followed by the rest of the code to set the variables. For context, I am doing a population model, so the initial values need to be set (option 1) before displaying (option 2), using (option 3) or

Is a constructor __init__ necessary for a class in Python?

萝らか妹 提交于 2021-01-21 08:28:56
问题 I read that the constructor is like the first argument passed to the class, which makes sense to me since the parameters seem to be passed to the class via the __init__ method. For example, class NewsStory(object): def __init__(self, guid, title, subject, summary, link): self.guid = guid self.title = title self.subject = subject self.summary = summary self.link = link def get_guid(self): return self.guid def get_title(self): return self.title def get_subject(self): return self.subject def get

Is a constructor __init__ necessary for a class in Python?

时光怂恿深爱的人放手 提交于 2021-01-21 08:28:28
问题 I read that the constructor is like the first argument passed to the class, which makes sense to me since the parameters seem to be passed to the class via the __init__ method. For example, class NewsStory(object): def __init__(self, guid, title, subject, summary, link): self.guid = guid self.title = title self.subject = subject self.summary = summary self.link = link def get_guid(self): return self.guid def get_title(self): return self.title def get_subject(self): return self.subject def get

Any way to force classes to have public static final field in Java?

时光总嘲笑我的痴心妄想 提交于 2021-01-21 07:48:06
问题 Is there a way to force classes in Java to have public static final field (through interface or abstract class)? Or at least just a public field? I need to make sure somehow that a group of classes have public static final String TYPE = "..."; in them. 回答1: No, you can't. You can only force them to have a non-static getter method, which would return the appropriate value for each subclass: public abstract String getType(); If you need to map each subclass of something to a value, without the

Is class an object in object oriented language

天涯浪子 提交于 2021-01-21 07:06:58
问题 Is class an object in object oriented language? How are Class methods accessed by just name of the class.method name ? (internal working). Is this same as a object.method ? And If the Class is same as object (belong to object class which is super class of every thing in OO) and we instantiate it (make object of it), can we make instance of an instance of an class other than Object class. (Mainly interested in the theoretical perspective even if practically not required ever) 回答1: Well, it

PHP - passing variables to classes

我怕爱的太早我们不能终老 提交于 2021-01-20 20:01:11
问题 I trying to learn OOP and I've made this class class boo{ function boo(&another_class, $some_normal_variable){ $some_normal_variable = $another_class->do_something(); } function do_stuff(){ // how can I access '$another_class' and '$some_normal_variable' here? return $another_class->get($some_normal_variable); } } and I call this somewhere inside the another_class class like $bla = new boo($bla, $foo); echo $bla->do_stuff(); But I don't know how to access $bla, $foo inside the do_stuff

PHP - passing variables to classes

两盒软妹~` 提交于 2021-01-20 19:58:08
问题 I trying to learn OOP and I've made this class class boo{ function boo(&another_class, $some_normal_variable){ $some_normal_variable = $another_class->do_something(); } function do_stuff(){ // how can I access '$another_class' and '$some_normal_variable' here? return $another_class->get($some_normal_variable); } } and I call this somewhere inside the another_class class like $bla = new boo($bla, $foo); echo $bla->do_stuff(); But I don't know how to access $bla, $foo inside the do_stuff