Jump to content

Python: classes and instances - how to treat this


dil_bert

Recommended Posts

hello dear PHP-Freaks,

 

 

well this seems to be a bit out of topic - but i want to dive into python  so i have some beginner questions:

 

 

in oo-development - lession classes and instances i have learned

 

in this video: at in

ff the developer says:

 

 

now these don´t need to be the same as our arguments, so for example i could make this
self dot  fname equals first, but usually i like to keep these similar if possible.
so i am going to go ahad and set that back to self dot first equals first, okay? . end of citation.

 


i wonder if it would be possible - to use other arguments than declarded

 

class Employee:

    def __init__(self, first, last, pay):
        self.first = first
        self.last = last
        self.email = first + '.' + last + '@email.com'
        self.pay = pay

    def fullname(self):
        return '{} {}'.format(self.first, self.last)

        
emp_1.first = `Corey`
emp_1.last  = `Schafer`
emp_1.email  = `corey.schafer@company.com `


so te question is. Can we really go and write like so:

emp_1.fname = `Corey`
emp_1.lname = `Schafer`
emp_1.email  = `corey.schafer@company.com `

but isn t this a lack of consistency?

 

love to hear from you.

greetings

Link to comment
Share on other sites

What he's saying is that the "first" and "last" from the constructor

def __init__(self, first, last, pay):
don't have to be the same name as the "first" and "last" on the object

self.first = first
self.last = last
Using "fname" and "lname" would be

def __init__(self, first, last, pay):
    self.fname = first
    self.lname = last
    self.email = first + '.' + last + '@email.com'
    self.pay = pay
That's allowed, Python doesn't care, but a human like me would read the code and think "why is one pair called first/last and the other called fname/lname"? It's odd.
Link to comment
Share on other sites

good evening dear requinix,

 

 

many thanks for the clearing.

 

 

 

That's allowed, Python doesn't care, but a human like me would read the code and think "why is one pair called first/last and the other called fname/lname"? It's odd.

 

 

you ve shed a ligth onto  this issue.

 

Thanks for helping me - here and so many times in other questions.

 

KEEP up  the superb work  - this forum is such a great place.

 

Greetings

dil_bert

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.