将变量写入类中。

huangapple go评论59阅读模式
英文:

how to write a variable into class

问题

我是Python类的新手所以我的问题是
我如何将这个代码

genome = {'speed': 0.03999485472564734, 
                    'max_speed': 0.26692713899472725,
                    'max_rotation': 0.04739626947680464, 
                    'track_time': 36}

写成一个类以便像这样访问

class Genome:
   ... genome字典在这里作为变量 ...

class Species():
  def __init__(self):
    ....

s = Species()
print(s.genome.speed)# 应该给我速度

也许我的方法不是正确的方法这个想法是拥有一个名为Species的类它可以复制自身然后将genome的所有值传递给子类但不传递其他任何东西
感谢你的帮助

我尝试简单地将genome传递为字典它可以工作但每次都像这样写很烦人
self.genome["speed"] 等... 我想摆脱["..."] 语法
或者如何以更聪明的方式做某事
英文:

I am new to Python classes, so my question is:
how can I write this:

genome = {'speed': 0.03999485472564734, 
                        'max_speed': 0.26692713899472725,
                        'max_rotation': 0.04739626947680464, 
                        'track_time': 36}

into a class, so that it can be accessed like this:

class Genome:
   ... the genome dict here as variables ...

class Species():
  def __init__(self):
    ....

s = Species()
print(s.genome.speed)# should give me the speed

maybe my approach is not the proper way. The idea is to have a Class Species, which can reproduce itself and then overgive all values from genome to the child, but not everything else?
thanks for your help

I tried to overgive genome simply as a dictionary, it works but it is annoying to write all the time like:
self.genome["speed"] etc... I want to get rid of the ["..."] syntax.
Or: how to do something in a smart way?

答案1

得分: 2

这是关于dataclass的说明:

@dataclass
class Genome():
    speed: float
    max_speed: float
    max_rotation: float
    track_time: int

不要像初始化字典一样初始化它:
self.genome = {"speed": ..., ...}

而应该这样做:self.genome = Genome(speed=..., max_speed=..., max_rotation=..., track_time=...)

顺便说一下,如果按顺序编写这些参数,就不需要在之前加上speed=track_time=

为了获得像你在评论中指出的那样的内容,你可以使用vars来获取类中的每个项目,并使用getattr(genome, name)来获取属性,例如:

for key in vars(genome):
    item = getattr(genome, key)
英文:

This is what a dataclass is for:

@dataclass
class Genome():
    speed: float
    max_speed: float
    max_rotation: float
    track_time: int

Instead of initilizing it like a dictionary:
self.genome = {"speed": ..., ...}

Do something like: self.genome = Genome(speed=..., max_speed=..., max_rotation=..., track_time=...)

Side note: You do not need the speed= or track_time= parts beforehand if you write these in order.

In order to get something like what you pointed out in the comment, you can use vars to get each item in the class and getattr(genome, name) to get the attribute like:

for key in vars(genome):
    item = getattr(genome, key)

答案2

得分: 1

以下是翻译好的代码部分:

genome = {'speed': 0.03999485472564734, 
          'max_speed': 0.26692713899472725,
          'max_rotation': 0.04739626947680464, 
          'track_time': 36}

class Genome:
    def __init__(self, genome):
        for k, v in genome.items():
            setattr(self, k, v)

class Species:
    def __init__(self):
        self.genome = Genome(genome)

x = Species()
print(x.genome.speed)

输出:

0.03999485472564734
英文:

You can try this:

genome = {'speed': 0.03999485472564734, 
          'max_speed': 0.26692713899472725,
          'max_rotation': 0.04739626947680464, 
          'track_time': 36}

class Genome:
    def __init__(self, genome):
        for k, v in genome.items():
            setattr(self, k, v)


class Species:
    def __init__(self):
        self.genome = Genome(genome)

For example:

x = Species()
print(x.genome.speed)

gives:

0.03999485472564734 

huangapple
  • 本文由 发表于 2023年2月27日 06:37:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575421.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定