Jump to content

Hello,

 

So I'm used to work with Laravel's Collection and Normal OOP PHP.

 

So currerntly I do 

 



data = Data.All()

for item in data['item']
	item['name']

but how should this be, is there some other way like PHP's way where you do etc data->items() and $item->name?

I also would like to change one of the values before it's returned, so I can do a if on a value, if it's this then change it with that and so on.

 

Does Pythons class stuff work the same or is it completely different?

 

EDIT:

 

The Data.All() returns a dict.

Back-end developer, electronics "hacker"

Link to comment
https://linustechtips.com/topic/907333-python-how-does-dict-and-classes-work/
Share on other sites

Link to post
Share on other sites

Are you trying to import a function from a library?

You can try this

# importing everything
from math import *

# import a specific function
from math import sqrt

# importing multiple functions
from math import sqrt, pi, sin

When importing something, don't add parenthesis, for example write sqrt not sqrt()

 

 

Hope that helped

Link to post
Share on other sites

Just now, lewdicrous said:

Are you trying to import a function from a library?

You can try this


# importing everything
from math import *

# import a specific function
from math import sqrt

# importing multiple functions
from math import sqrt, pi, sin

When importing something, don't add parenthesis, for example write sqrt not sqrt()

 

 

Hope that helped

Nope you missed the whole thread.

Back-end developer, electronics "hacker"

Link to post
Share on other sites

I tried, this is the part I want.

 

class Dog:

    kind = 'canine'         # class variable shared by all instances

    def __init__(self, name):
        self.name = name    # instance variable unique to each instance

>>> d = Dog('Fido')
>>> e = Dog('Buddy')
>>> d.kind                  # shared by all dogs
'canine'
>>> e.kind                  # shared by all dogs
'canine'
>>> d.name                  # unique to d
'Fido'
>>> e.name                  # unique to e
'Buddy'

But I can't set the self outside the __init__.

 

etc full code:

 

data = Data.Request()
items = data.all()
for item in items:
  item['name']

in this I would like to do etc item.name

and I would like to have the value returned in item.status to be changed before it's returned.

 

4 minutes ago, Dujith said:

Do you mean a dictionary with dict?

Correct.

Back-end developer, electronics "hacker"

Link to post
Share on other sites

  • 2 weeks later...
On 19/03/2018 at 12:41 PM, Joveice said:

I tried, this is the part I want.

 

<snip>

 

But I can't set the self outside the __init__.

 

etc full code:

 


data = Data.Request()
items = data.all()
for item in items:
  item['name']

in this I would like to do etc item.name

and I would like to have the value returned in item.status to be changed before it's returned.

 

Correct.

Not quite sure what you are trying to achieve. Do you want dictionaries or classes? How is item.status related to item.name?

 

Class

Anyway, the "self" parameter refers to the object itself, which is accessible inside the class and is passed to each method call, as the first argument, implicitely. You cannot "set" self to anything, since it refers to the object (e.g. d or e in your example). You can change or add an attribute by just doing

d.name = 'New name'	# Change existing attribute.
d.breed = 'New dog'	# Add new attribute.

Dictionaries

Dictionaries are just stored as

example_dict = {'key1':value1, 'key2':value2, 'key3':value3}

and can be accessed/modified/extended with

example_dict['key1']	# Get the value.
example_dict['key1'] = <new value>	# Modify the value of key1.
example_dict['key4'] = <value>	# Add a new element.

To loop over all the items you can do something like

for key, value in example_dict.items():# or items() is Python3, replace with iteritems in Python 2
  if key == 'something':
    new_value = value * 5
    example_dict[key] = new_value

 

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×