python def init self_Python类不使用def __init __(self)

I am writing a series of text menus. With the class and sub class below it runs with no issues. But I am reviewing my coding and I am wondering....is it ok that I didn't use def __init__(self) in the classes? Should I have placed the data members in def __init__(Self): such as self.images = (), self.options =()? If I did that then I could not use the abc module for restrains, correct?

class BaseMenu(object):

__metaclass__ = abc.ABCMeta

@abc.abstractproperty

def options(self):

pass

@abc.abstractproperty

def menu_name(self):

pass

def display(self):

header = "FooBar YO"

term = getTerminalSize()

#sys.stdout.write("\x1b[2J\x1b[H")

print header.center(term, '*')

print self.menu_name.center(term, '+')

print "Please choose which option:"

for i in self.options:

print(

str(self.options.index(i)+1) + ") "

+ i.__name__

)

value = int(raw_input("Please Choose: ")) - 1

self.options[value](self)

class Servers(BaseMenu):

menu_name = "Servers"

images = ()

foo = ()

def get_images(self):

if not self.images:

self.images = list_images.get_images()

for img in self.images:

print (

str(self.images.index(img)+1) + ") "

+ "Name: %s\n ID: %s" %

(img.name, img.id)

)

def get_foo(self):

if not self.foo:

self.foo = list_list.get_list()

for list in self.foo:

print "Name:", list.name

print " ID:", list.id

print

def create_servers(self):

create_server.create(self)

options = (

get_images,

get_foo,

create_servers

)

解决方案

Your code is perfectly fine. You don't have to have an __init__ method.

You can still use __init__, even with an ABC. All that the ABC meta tests for is if the names have been defined. Setting images in an __init__ does requires that you define a class attribute, but you can set that to None at first:

class Servers(BaseMenu):

menu_name = "Servers"

images = None

foo = None

def __init__(self):

self.images = list_images.get_images()

self.foo = list_list.get_list()

Now you can set constraints on the ABC requiring that a images abstract property be available; the images = None class attribute will satisfy that constraint.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中,`def __init__(self):`是定义类的构造函数(initializer)的方式之一。构造函数是在创建一个类的实例时被调用的方法,用于初始化对象的属性。 构造函数通常会使用`self`作为第一个参数,表示实例本身。通过`self`可以访问并操作实例的属性。在构造函数中,可以使用`self.属性名 = 初始值`的方式给实例的属性赋初值。 然而,在Python中,也可以使用其他参数来定义构造函数,在函数定义中指定这些参数即可。这种方式可以在创建类的实例时直接传入初始值,而不需要在构造函数内部进行赋值操作。 举个例子,考虑以下两种定义`__init__`方法的方式: 方式一: ```python class Person: def __init__(self): self.name = None self.gender = None self.age = None ``` 方式二: ```python class Person: def __init__(self, name, gender, age): self.name = name self.gender = gender self.age = age ``` 在方式一中,构造函数没有接收任何参数,而是在函数体内部通过`self`给实例的属性赋初值。在创建`Person`类的实例时,可以先创建一个空的实例,然后逐个给属性赋值。 在方式二中,构造函数接收三个参数,这些参数分别用于初始化实例的`name`、`gender`和`age`属性。在创建`Person`类的实例时,需要传入这三个参数的值,以便对属性进行初始化。 所以,`def __init__(self, name, gender, age):`是一种更灵活的构造函数定义方式,可以直接在创建实例时传入初始值,避免了在构造函数内部逐个赋值的过程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值