This question already has an answer here:

Why does adding a trailing comma after a variable name make it a tuple? 6 answers

I have a very simple class:

class Item(object): def __init__(self, guid, sku, name): self.guid = guid, self.sku = sku, = name

When I create an object, such as:

item1 = Item('abc123', 1, 'abc1')

And I do:

print item1.guid

I get this as an output:


Instead of the expected:


Any idea why it does this?

P.S.: name works as expected!

self.guid = guid, # Comma means create a tuple! self.sku = sku, = name

You have done this yourself in your __init__ code. The tuple is constructed by a comma. Parentheses are just there to avoid ambiguity in some cases.

The correct code looks like this:

self.guid = guid self.sku = sku = name

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

本文标题:The __init__ class takes par ...

技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(17)