叉和分支在github上的区别(difference between fork and branch on github)
如果我分叉一个托管在github上的项目。 我分叉所有的分支吗? 我如何知道我的叉是哪个分支? 换句话说,哪个分支将被下载到我的电脑?
If I fork a project that's hosted on github. Do I fork all the branches? How do I know which branch my fork is based on? In other words which branch will be downloaded to my PC?
原文:https://stackoverflow.com/questions/5009600
最满意答案
您可以使用
@property
装饰器来管理您的type
属性:class Ship(object): def __init__(self, type): self.fixed = define_direction() self.row = random_row(board, self) self.col = random_col(board, self) self.type = type print "Type:", str(self.type) @property def type(self): return { "Patrol boat": 0, "Destroyer": 1, "Submarine": 2, "Battleship": 3, "Aircraft carrier": 4, }.get(self._type, 0) @type.setter def type(self, type): self._type = { 0: "Patrol boat", 1: "Destroyer", 2: "Submarine", 3: "Battleship", 4: "Aircraft carrier", }.get(type, "Patrol boat") def __repr__(self): return "Ship. Type: " + self._type
你的
__repr__
应该总是返回一个字符串; 你回来了一个元组。 您的错误是由您的self.type()
调用引起的; 因为self.type
在您的代码中存储了一个字符串,所以您试图将该字符串视为可调用的。可以从
__init__
调用其他函数(类之外); 它只是你的实例上的另一种方法,只考虑具有和尚未设置的属性。 但是,如果函数依赖于self
信息并且在类外没有用处,我会将它移动到带有_
前缀的类中,以指示它是类实现的内部。You can use a
@property
decorator to manager yourtype
attribute:class Ship(object): def __init__(self, type): self.fixed = define_direction() self.row = random_row(board, self) self.col = random_col(board, self) self.type = type print "Type:", str(self.type) @property def type(self): return { "Patrol boat": 0, "Destroyer": 1, "Submarine": 2, "Battleship": 3, "Aircraft carrier": 4, }.get(self._type, 0) @type.setter def type(self, type): self._type = { 0: "Patrol boat", 1: "Destroyer", 2: "Submarine", 3: "Battleship", 4: "Aircraft carrier", }.get(type, "Patrol boat") def __repr__(self): return "Ship. Type: " + self._type
Your
__repr__
should always return a string; you were returning a tuple instead. Your error was caused by yourself.type()
call; sinceself.type
stores a string in your code, you were trying to treat that string as a callable.It is fine to call other functions (outside of the class) from
__init__
; it is just another method on your instance really, just take into account what attributes have and haven't been set yet. However, if the function relies on information onself
and has no uses outside the class, I'd move it into the class with a_
prefix to indicate that it is internal to the class implementation.
相关问答
更多-
那么这是一个经典的讨论。 Stack Overflow中有几个其他的线程关于这个。 但。 Get / Set的(自动属性?)并不全是坏的。 但是他们往往会让你将实体构建为只有prop和not方法的“死”数据容器。 这种迹象通常被称为贫血领域 - 并且行为很少。 我的建议是: 尝试尽可能少地使用道具。 尝试找到属于一起的数据组,应该像前一样。 名字中间名和姓氏。 另一个例子是Zipcode,City,Street。 这些数据最好通过一种方法来设置。 它最大限度地减少了您的实体无效的机会。 通常属于一起的数据可 ...
-
使用_ClassName__attribute ,您可以访问该属性: >>> class MyClass(object): ... def __init__(self): ... self.__x = 3 ... @property ... def x(self): ... return self.__x ... @x.setter ... def x(self, value): ... if value != 3: . ...
-
您可以使用@property装饰器来管理您的type属性: class Ship(object): def __init__(self, type): self.fixed = define_direction() self.row = random_row(board, self) self.col = random_col(board, self) self.type = type print "Type:", s ...
-
喜欢的属性 。 这是他们在那里。 原因是Python中的所有属性都是公共的。 使用下划线或两个起始名称只是一个警告,指定的属性是在将来版本的代码中可能不一致的实现细节。 它不会阻止您实际获取或设置该属性。 因此,标准属性访问是正常的,Pythonic的方式,很好的访问属性。 属性的优点是它们与属性访问的语法相同,因此您可以从一个更改到另一个,而不会对客户端代码进行任何更改。 你甚至可以有一个版本的类使用属性(例如,按代码分包或调试)和一个不用于生产的版本,而不需要更改使用它的代码。 同时,您不必为所有事务编 ...
-
实际上有很多很好的理由考虑使用访问器,而不是直接暴露一个类的字段,而不仅仅是封装的参数,并且使未来的更改更容易。 以下是我所知道的一些原因: 封装与获取或设置属性相关的行为 - 这允许稍后更容易添加其他功能(如验证)。 在使用替代表示方式暴露属性时隐藏属性的内部表示。 将公共界面与变更保持一致 - 允许公共界面保持不变,而实施变更而不影响现有消费者。 控制属性的生命周期和内存管理(处置)语义 - 在非托管内存环境(如C ++或Objective-C)中尤为重要)。 提供调试截取点,以便在运行时更改属性时调试 ...
-
请参阅Python内置日志记录模块。 没有必要重新发明轮子。 以下是文档中的示例: import logging logging.warning('Watch out!') # will print a message to the console logging.info('I told you so') # will not print anything 如果要设置日志级别,以便打印INFO +更高优先级的消息: logging.getLogger().setLevel(logging.INFO) ...
-
Setters / Getters(AS3)(Setters / Getters (AS3))[2022-07-07]
发生这种情况是因为您已经为MainPage和MiniGameOne类定义并检查nextLevel 。 当您将miniGameOne实例中的级别更改为2 ,它将变为2直到您将其更改为其他号码。 从这一点你有: mainPage.getNextLevel() == 1和miniGameOne.getNextLevel() == 2 所以在你的enterFrame()这两个条件都是真实的,你的miniGameOne被indeen添加到舞台上,但是在同一帧中它被删除,并且你的mainPage被再次添加。 作为最快的 ... -
如果你关心覆盖率指标,那么setter和getter可以杀死你。 无论如何,你绝对应该测试它们,特别是如果它们不仅仅是设置或获得它们。 此外,它可以在一次测试中测试set / get。 如果道具是私人的,那么你必须这样做,除非有一套副作用。 或者,您可以在测试其他方法时隐式测试它们。 例如,如果您正在测试DAO,则可以在编写testSave方法时使用setter和getter ... 为了使生活更轻松,您可以编写基于反射的测试实用程序来测试setter和getter。 或者只是编写简单的测试,虽然很无聊,但 ...
-
您的好习惯是将您的逻辑包装到Service中 。 您必须知道,在Angular中,所有服务都是Singleton ,只有一个Service实例。 我做了一个简单的例子,使用$ q.defer() ,它是延迟API的promise管理器 。 $ q.defer()获得2种方法: resolve(value):通过给她最终值来解决我们的相关承诺 拒绝(原因):解决承诺错误。 调节器 (function(){ function Controller($scope, $q, Service) { //Use ...
-
在代码示例中,我发现它们只有一个getter,但不是一个setter。 但我真的不明白为什么。 您描述的功能是在创建Person对象时,没有人可以set其gender和length 。 如果用户希望,他/她可以根据需要创建具有属性的新 Person ( new Person(...) )。 但是在创建Person之后,您无法set属性。 但是this.gender = gender可以作为制定者工作? 我似乎无法弄清楚它的功能是什么。 它确实可以作为一个setter(虽然它本身不是一个setter,因为它不 ...